【发布时间】:2017-12-31 12:37:17
【问题描述】:
所以我有一个 .csv 文件,其中每一行如下所示:
,11:00:14,4,5.,93.7,0.01,0.0,7,20,0.001,10,49.3,0.01,
,11:00:15,4,5.,94.7,0.04,0.5,7,20,0.005,10,49.5,0.04,
什么时候应该是这样的:
11:00:14,4,5.,93.7,0.01,0.0,7,20,0.001,10,49.3,0.01
11:00:15,4,5.,94.7,0.04,0.5,7,20,0.005,10,49.5,0.04
我认为这就是 pandas 没有正确创建数据帧的原因。如何删除这些逗号?
生成原始csv文件的代码是
def tsv2csv():
# read tab-delimited file
with open(file_location + tsv_file,'r') as fin:
cr = csv.reader(fin, delimiter='\t')
filecontents = [line for line in cr]
# write comma-delimited file (comma is the default delimiter)
# give the exact location of the file
#"newline=''" at the end of the line stops there being spaces between each row
with open(new_csv_file,'w', newline='') as fou:
cw = csv.writer(fou, quotechar='', quoting=csv.QUOTE_NONE)
cw.writerows(filecontents)
【问题讨论】:
-
生成原始CSV文件的代码是什么?
-
我已经在主帖中添加了代码
-
如果您的问题得到解答,请vote on, and accept the most helpful one。您可以通过单击最有帮助的答案旁边的灰色复选标记并将其变为绿色来接受答案。谢谢。
标签: python pandas csv dataframe comma