我有以下代码输出到csv。
import csv
keys = ['Name','Hour','Time']
dirname = os.path.dirname(os.path.abspath(__file__))
csvfilename = os.path.join(dirname, 'CSVFile.csv')
with open(csvfilename, 'wb') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(final)
其中final
是表格
final = [{'Name':'A','Hour':0,'Time':120},{'Name':'A','Hour':1,'Time':219},...,{'Name':'B','Hour':0,'Time':10},...]
如何将final输出为.tsv格式?
答案 0 :(得分:2)
您可以使用DictWriter
选项在delimiter
中指定:
dict_writer = csv.DictWriter(output_file, keys, delimiter='\t')
或使用dialect
:
dict_writer = csv.DictWriter(output_file, keys, dialect='excel-tab')