我有一个大文本文件,想用Python将其转换为CSV格式。我的数据如下:
var1|var2|var3|tonumber|fromnumber|var|coding|udh|var|circle|var|var|var|var15
898980d1-6e5b-40f2-a313-c30f08bf0fe6|49A5919EB0D04EDE9B6CEB5AF932EAA3|sbs1|919899980898|HITECH|1|1|0|VODAFONE|Delhi|2015-02-21 12:08:51|5|3|RBA/6724R # Kailash Ram Panwar (PL) # Rz-410/13 Flat No-09 Iiird Floor Tkd Extn Delhi - 110019-110019 # Tgt Skt # #
我该如何将这个文件转换为CSV格式?我尝试了以下方法:
In [1]: import csv
In [2]: import pandas as pd
In [3]: piperows = []
f = open("/home/suri/ValueFirst/MT.txt", "rb")
In [6]: readerpipe = csv.reader(f, delimiter = '|')
In [7]: for row in readerpipe:
...: piperows.append(row)
...: f.close()
...:
我遇到了以下错误:
----------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-842b0d42f436> in <module>()
----> 1 for row in readerpipe:
2 piperows.append(row)
3 f.close()
4
ValueError: I/O operation on closed file
header1|header2|header3
var1|var2|var3
的文件转换成header1,header2,header3
var1,var2,var3
。如果您需要其他内容,请编辑您的问题以使其更清晰:这比行内注释更易读 ;) - snooze92