【发布时间】:2024-04-23 06:15:02
【问题描述】:
我正在尝试解析一系列文本文件并使用 Python (2.7.3) 将它们保存为 CSV 文件。所有文本文件都有一个 4 行长的标题,需要去掉。数据行有各种分隔符,包括“(引号)、-(破折号)、: 列和空格。我发现在 C++ 中使用所有这些不同的分隔符对其进行编码很痛苦,所以我决定在 Python 中尝试一下与 C/C++ 相比,做起来相对容易。
我编写了一段代码来测试它是否适用于单行数据并且它可以工作,但是我无法让它适用于实际文件。为了解析单行,我使用了文本对象和“替换”方法。看起来我当前的实现将文本文件作为列表读取,并且列表对象没有替换方法。
作为 Python 新手,我被困在了这一点上。任何意见将不胜感激!
谢谢!
# function for parsing the data
def data_parser(text, dic):
for i, j in dic.iteritems():
text = text.replace(i,j)
return text
# open input/output files
inputfile = open('test.dat')
outputfile = open('test.csv', 'w')
my_text = inputfile.readlines()[4:] #reads to whole text file, skipping first 4 lines
# sample text string, just for demonstration to let you know how the data looks like
# my_text = '"2012-06-23 03:09:13.23",4323584,-1.911224,-0.4657288,-0.1166382,-0.24823,0.256485,"NAN",-0.3489428,-0.130449,-0.2440527,-0.2942413,0.04944348,0.4337797,-1.105218,-1.201882,-0.5962594,-0.586636'
# dictionary definition 0-, 1- etc. are there to parse the date block delimited with dashes, and make sure the negative numbers are not effected
reps = {'"NAN"':'NAN', '"':'', '0-':'0,','1-':'1,','2-':'2,','3-':'3,','4-':'4,','5-':'5,','6-':'6,','7-':'7,','8-':'8,','9-':'9,', ' ':',', ':':',' }
txt = data_parser(my_text, reps)
outputfile.writelines(txt)
inputfile.close()
outputfile.close()
【问题讨论】:
-
你应该附上一份你需要解析的文件和预期的输出,这样会更容易帮助你。
标签: python parsing text file-io python-2.7