如何从文本文件读取列表,该列表位于python中某些文本的下面?

时间:2018-06-21 10:26:03

标签: python regex

这是一个文本文件,我必须从中提取python列表(从[{“ WW”:“ WW18” ... ...开始),并且它们在该列表上执行一些操作。该怎么做?

Verified Title Successfully
6
Title of the graph Firmware Trend Status - AB - WW24.7
Title of the graph with categoryFirmware Trend Status - AB - WW24.7 
###########Firmware Weekly Status Value############
[{"WW":"WW18","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":45,"passPerc":0,"runPerc":15},{"WW":"WW19","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":25,"passPerc":0,"runPerc":15},{"WW":"WW20","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":35,"passPerc":0,"runPerc":15},{"WW":"WW21","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":45,"passPerc":0,"runPerc":15},{"WW":"WW22","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":55,"passPerc":0,"runPerc":15},{"WW":"WW23","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":75,"passPerc":0,"runPerc":15},{"WW":"WW24","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":65,"passPerc":0,"runPerc":15},{"WW":"WW25","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0},{"WW":"WW26","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0},{"WW":"WW27","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0},{"WW":"WW28","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0},{"WW":"WW29","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0},{"WW":"WW30","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0}]

编辑: 我尝试了以下代码:

import re
import ast
fo=open(r"D:\Combined2345.txt","r")
s=fo.read()
data = re.search("\[.*?\]", s)
if data:
    print( ast.literal_eval(data.group()))

但显示错误:

Traceback (most recent call last):
  File "D:/com12.py", line 7, in <module>
    print( ast.literal_eval(data.group()))
  File "C:\Users\2015s\AppData\Local\Programs\Python\Python36-32\lib\ast.py", line 85, in literal_eval
    return _convert(node_or_string)
  File "C:\Users\2015s\AppData\Local\Programs\Python\Python36-32\lib\ast.py", line 61, in _convert
    return list(map(_convert, node.elts))
  File "C:\Users\2015s\AppData\Local\Programs\Python\Python36-32\lib\ast.py", line 84, in _convert
    raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x07C9D230>

原因是什么?

1 个答案:

答案 0 :(得分:1)

使用regexast模块。

演示:

import re
import ast
s = """Verified Title Successfully
6
Title of the graph Firmware Trend Status - AB - WW24.7
Title of the graph with categoryFirmware Trend Status - AB - WW24.7 
###########Firmware Weekly Status Value############
[{"WW":"WW18","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":45,"passPerc":0,"runPerc":15},{"WW":"WW19","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":25,"passPerc":0,"runPerc":15},{"WW":"WW20","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":35,"passPerc":0,"runPerc":15},{"WW":"WW21","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":45,"passPerc":0,"runPerc":15},{"WW":"WW22","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":55,"passPerc":0,"runPerc":15},{"WW":"WW23","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":75,"passPerc":0,"runPerc":15},{"WW":"WW24","Pass":0,"Fail":8,"Notrun":47,"Notready":0,"total":65,"passPerc":0,"runPerc":15},{"WW":"WW25","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0},{"WW":"WW26","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0},{"WW":"WW27","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0},{"WW":"WW28","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0},{"WW":"WW29","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0},{"WW":"WW30","Pass":0,"Fail":0,"Notrun":0,"Notready":0,"total":0,"passPerc":0,"runPerc":0}]
"""

data = re.search("\[.*?\]", s)              #Get content between []
if data:
    print( ast.literal_eval(data.group()))  #Convert string to list object.

输出:

[{'runPerc': 15, 'Notready': 0, 'WW': 'WW18', 'passPerc': 0, 'Pass': 0, 'Fail': 8, 'total': 45, 'Notrun': 47}, {'runPerc': 15, 'Notready': 0, 'WW': 'WW19', 'passPerc': 0, 'Pass': 0, 'Fail': 8, 'total': 25, 'Notrun': 47}, {'runPerc': 15, 'Notready': 0, 'WW': 'WW20', 'passPerc': 0, 'Pass': 0, 'Fail': 8, 'total': 35, 'Notrun': 47}, {'runPerc': 15, 'Notready': 0, 'WW': 'WW21', 'passPerc': 0, 'Pass': 0, 'Fail': 8, 'total': 45, 'Notrun': 47}, {'runPerc': 15, 'Notready': 0, 'WW': 'WW22', 'passPerc': 0, 'Pass': 0, 'Fail': 8, 'total': 55, 'Notrun': 47}, {'runPerc': 15, 'Notready': 0, 'WW': 'WW23', 'passPerc': 0, 'Pass': 0, 'Fail': 8, 'total': 75, 'Notrun': 47}, {'runPerc': 15, 'Notready': 0, 'WW': 'WW24', 'passPerc': 0, 'Pass': 0, 'Fail': 8, 'total': 65, 'Notrun': 47}, {'runPerc': 0, 'Notready': 0, 'WW': 'WW25', 'passPerc': 0, 'Pass': 0, 'Fail': 0, 'total': 0, 'Notrun': 0}, {'runPerc': 0, 'Notready': 0, 'WW': 'WW26', 'passPerc': 0, 'Pass': 0, 'Fail': 0, 'total': 0, 'Notrun': 0}, {'runPerc': 0, 'Notready': 0, 'WW': 'WW27', 'passPerc': 0, 'Pass': 0, 'Fail': 0, 'total': 0, 'Notrun': 0}, {'runPerc': 0, 'Notready': 0, 'WW': 'WW28', 'passPerc': 0, 'Pass': 0, 'Fail': 0, 'total': 0, 'Notrun': 0}, {'runPerc': 0, 'Notready': 0, 'WW': 'WW29', 'passPerc': 0, 'Pass': 0, 'Fail': 0, 'total': 0, 'Notrun': 0}, {'runPerc': 0, 'Notready': 0, 'WW': 'WW30', 'passPerc': 0, 'Pass': 0, 'Fail': 0, 'total': 0, 'Notrun': 0}]