Python 将字典列表保存到.tsv文件
我有以下代码要输出到csvPython 将字典列表保存到.tsv文件,python,python-2.7,Python,Python 2.7,我有以下代码要输出到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,
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格式?您可以在
DictWriter
中使用分隔符
选项指定:
dict_writer = csv.DictWriter(output_file, keys, delimiter='\t')
或者使用方言
:
dict_writer = csv.DictWriter(output_file, keys, dialect='excel-tab')
您可以在
DictWriter
中使用delimiter
选项指定:
dict_writer = csv.DictWriter(output_file, keys, delimiter='\t')
或者使用方言
:
dict_writer = csv.DictWriter(output_file, keys, dialect='excel-tab')
非常感谢。另外,更改了csvfilename=os.path.join(dirname,'TSVFile.tsv')谢谢。另外,更改了
csvfilename=os.path.join(dirname,'TSVFile.tsv')