这是一个文本文件,我必须从中提取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>
原因是什么
使用
regex
和ast
模块演示:
输出:
相关问题 更多 >
编程相关推荐