将列表写入.xls文件的最"pythonic"方式? [英] Most 'pythonic' way to write list to .xls file?
本文介绍了将列表写入.xls文件的最"pythonic"方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
the_list = [['a','b','c'],['b','c','d'],['c','d','e']]
output = []
for k in the_list:
output.append(str(k)[1:-1].replace(',',"\t").replace("'",'').replace(' ',''))
print output
#['a\tb\tc', 'b\tc\td', 'c\td\te']
#for line in output:
#out_file.write(line + '\n')
我正在尝试将列表转换为制表符允许的格式,以便可以写入.xls文件,但是我想出办法的唯一方法似乎很单调.我想知道是否有人知道一种更快,更有效和更"pythonic"的方式将列表写入.xls
I'm trying to get the list into tab-delmited format so i can write to an .xls file but the only way i could figure out how to do it seemed pretty monotonous . I was wondering if anyone knew a quicker, more efficient, and more 'pythonic' way of writing a list to an .xls
推荐答案
您可以通过列表理解来完成所需的格式
You can accomplish the formatting you're looking for with a list comprehension
output = ["\t".join(a) for a in the_list]
请参见 join 上的python文档.
See python's documentation on join.
这篇关于将列表写入.xls文件的最"pythonic"方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文