【发布时间】:2016-06-07 01:43:51
【问题描述】:
我正在编写一个 python 脚本来审核来自我公司的许多不同应用程序的所有 CSV 文件,我遇到了一点麻烦,我几乎完成了一个概念证明,并提交给老板,并且炫耀我可以用 python 做什么,但问题是我不太了解 python 中的 CSV 类....
这是我的计算机信息列表的示例:
['EB-ABORTZ,True,False,False,0', 'EB-AGONCHAROVA,True,False,False,0',
'EB-AHART-1,True,False,False,0', 'EB-AHEIDENREICH,True,False,False,0',
'EB-ALOCKLEAR,True,False,False,0', 'EB-AMARGULIS,True,False,False,0',
'EB-ASKLAR,True,False,False,0', 'EB-ASKLAR-1,True,False,False,0',
'EB-ASKLAR-3,True,False,False,0', 'EB-BCHOW-1,True,False,False,0',
'EB-BJOHNSON,True,False,False,0', 'EB-BLYLE,True,False,False,0',
'EB-BRUSSUM,True,False,False,0', 'EB-CCLEARY,True,False,False,0',
'EB-...]
这是最后代码生成的示例.....
"E","B","-","A","B","O","R","T","Z",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
"E","B","-","A","G","O","N","C","H","A","R","O","V","A",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
"E","B","-","A","H","A","R","T","-","1",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
"E","B","-","A","H","E","I","D","E","N","R","E","I","C","H",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
"E","B","-","A","L","O","C","K","L","E","A","R",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
"E","B","-","A","M","A","R","G","U","L","I","S",",","T","r","u","e",",","F","a","l","s","e",",","F","a","l","s","e",",","0"
这是我在事后尝试将数据导出为 CSV 格式的方法的副本。
def collate_computers(computers):
with open('results.csv', 'w', encoding='utf8', )as outfile:
writer = csv.writer(outfile, quoting=csv.QUOTE_ALL, delimiter=',')
for c in computers:
writer.writerow(c)
【问题讨论】:
-
您的问题到底是什么?我可以正确使用您的代码。你的预期输出是什么?
标签: python python-3.x csv export-to-csv