【问题标题】:Regex issues while parsing text file using Python使用 Python 解析文本文件时出现正则表达式问题
【发布时间】:2016-11-08 17:30:40
【问题描述】:

有人可以帮助我在通过正则表达式解析文本文件时做错了什么吗?使用 Python 执行代码。我在下面的文本文件中有一个响应,我想解析并获取 numvaluelist 值。当前收到 TypeError。

错误:

lines = line_re.findall(data)
TypeError: expected string or buffer

字符串格式的文本文件 (.txt)

historic_list {
  id: "Text1(long) 11A"
  startdate: 345453
  numvaluelist: 0.123
  datelist: 345453
}
historic_list {
  id: "Text1(short) 11B"
  startdate: 345453
  numvaluelist: 0.456
  datelist: 345453
}
historic_list {
  id: "Text2(long) 11C"
  startdate: 345453
  numvaluelist: 1.789
  datelist: 345453
}
datelist: 345453
}
time_statistics {
  job_id: "123"
}
UrlPairList {
}

Python 代码

f= open(".txt_file", "r")
data = f.readlines()
# print data

line_re = re.compile(r'\{[^\}]+\}')
value_re = re.compile(r"(\w+): ('[^']*'|\S+)")

results = []
lines = line_re.findall(data)
for line in lines:
    data_line = dict()
    values = re.findall(value_re, line)
    for (name, value) in values:
        if(value[-1] == '}'): value = value[:-1]  # to handle "foo}" without space
        if(value[:1] == "'"): value = value[1:-1]  # strip quotes
        data_line[name] = value
    results.append(data_line)

print type(results)

final_results = []
for i in results:
    for key, value in i.items():
        if key == 'numvaluelist':
            final_results.append(i['numvaluelist'])
print final_results

【问题讨论】:

    标签: python regex parsing


    【解决方案1】:

    问题不在于您的正则表达式。 readlines 返回一个列表,但 re.findall 接受一个字符串或缓冲区。

    你想要的是:

    data = f.read()
    

    将文件内容作为单个字符串返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 2013-08-13
      • 1970-01-01
      相关资源
      最近更新 更多