Python - Nested List to Tab Delimited File?

2019-03-27 15:49发布

I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g.,

nested_list = [['x', 'y', 'z'], ['a', 'b', 'c']].

I wish to create a function in order to output this data construct into a tab delimited format, e.g.,

x    y    z
a    b    c

Any help greatly appreciated!

Thanks in advance, Seafoid.

5条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-27 16:19
>>> nested_list = [['x', 'y', 'z'], ['a', 'b', 'c']]
>>> for line in nested_list:
...   print '\t'.join(line)
... 
x   y   z
a   b   c
>>> 
查看更多
祖国的老花朵
3楼-- · 2019-03-27 16:23

In my view, it's a simple one-liner:

print '\n'.join(['\t'.join(l) for l in nested_list])
查看更多
可以哭但决不认输i
4楼-- · 2019-03-27 16:29
>>> print '\n'.join(map('\t'.join,nested_list))
x       y       z
a       b       c
>>>
查看更多
贪生不怕死
5楼-- · 2019-03-27 16:33
with open('fname', 'w') as file:
    file.writelines('\t'.join(i) + '\n' for i in nested_list)
查看更多
Emotional °昔
6楼-- · 2019-03-27 16:42
out = file("yourfile", "w")
for line in nested_list:
    print >> out, "\t".join(line)
查看更多
登录 后发表回答