导出为Python的CSV文件格式
问题描述:
我想导出一些CsV格式的内容..但无法做到。导出为Python的CSV文件格式
下面是我在做python的任意想法。
script , file = argv
emails = open(file,"r") #opens the file
results = csv.writer(open("Results.txt" , "w")) #Creates a new file to show results.
resultsList = []
here is the complete data and all
for results in resulsList:
results.writeline(result)
results.close()
emails.close()
现在......我需要将所有存储在results.txt中的数据以CSV格式保存。
请提供您对此的反馈。 谢谢。
答
你可以使用numpy的genfromtxt
和savetxt
:
import numpy as np
np.savetxt('results.csv', np.genfromtxt(file,delimiter='',dtype=None),delimiter=',')
注意您必须在genfromtxt
适当的分隔符什么是你写的文件?您可以使用您用于编写Results.txt的类似方法将所有数据导出到文件。请确保每个字段之间用逗号分隔(请参阅http://en.wikipedia.org/wiki/Comma-separated_values以获取CSV格式的基本规则) – Learner 2014-10-30 08:53:00
不要使用'results.close()',请使用'打开(“Results.txt”,“r”)如inFile:'。良好的做法;)什么是问题?您只需从文件中读取数据,解析它,然后转储到另一个文件中。 – 2014-10-30 08:55:50
看起来你的'for'语句后有一个缩进错误... – atomh33ls 2014-10-30 08:56:02