将文本作为注释而不是字符串读取
read text as comment instead of string
我得到了一些文本文件,格式如下:
{"MessageType":"33 (SM data)","REG":"BUL","TS":"2022-02-24T11:02:00","M":false,"LP":3,"CT":119,"SM":[{"LM":602,"SA/LK":67,"RT":"Strategic Approach Record","SG":"1","PT":52,"Lanes":[{"L":1,"MF":15},{"L":2,"MF":19}]},{"LM":602,"SA/LK":68,"RT":"Strategic Approach Record","SG":"2","PT":28,"Lanes":[{"L":1,"MF":7},{"L":2,"MF":6}]},{"LM":602,"SA/LK":69,"RT":"Strategic Approach Record","SG":"3","PT":17,"Lanes":[{"L":1,"MF":2}]},{"LM":602,"SA/LK":70,"RT":"Strategic Approach Record","SG":"4","PT":70,"Lanes":[{"L":1,"MF":12},{"L":2,"MF":4}]},{"LM":602,"SA/LK":3,"RT":"Link Record","SG":"1","PT":52,"Lanes":[{"L":1,"MF":15},{"L":2,"MF":19}]},{"LM":602,"SA/LK":4,"RT":"Link Record","SG":"4","PT":70,"Lanes":[{"L":1,"MF":12},{"L":2,"MF":4}]}],"SP":[{"A":"(Stretch)33%","B":"20%","C":"27%","D":"20%"}]}
有什么方法可以将文本拆分成如下所示的内容:
[{'LM': 602,
'SA/LK': 67,
'RT': 'Strategic Approach Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 68,
'RT': 'Strategic Approach Record',
'SG': '2',
'PT': 28,
'Lanes': [{'L': 1, 'MF': 7}, {'L': 2, 'MF': 6}]},
{'LM': 602,
'SA/LK': 69,
'RT': 'Strategic Approach Record',
'SG': '3',
'PT': 17,
'Lanes': [{'L': 1, 'MF': 2}]},
{'LM': 602,
'SA/LK': 70,
'RT': 'Strategic Approach Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]},
{'LM': 602,
'SA/LK': 3,
'RT': 'Link Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 4,
'RT': 'Link Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]}]
这样我就可以轻松地将其转换为列表或字典...
我试过使用 string.split(','),但实际上不起作用 - 因为它会拆分每个 ','
看起来像 JSON。使用 built-in json
库解析成 Python 对象:
In [1]: import json
In [2]: data = '...' # string provided by OP, read from file
In [3]: json.loads(data)
Out[3]:
{'MessageType': '33 (SM data)',
'REG': 'BUL',
'TS': '2022-02-24T11:02:00',
'M': False,
'LP': 3,
'CT': 119,
'SM': [{'LM': 602,
'SA/LK': 67,
'RT': 'Strategic Approach Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 68,
'RT': 'Strategic Approach Record',
'SG': '2',
'PT': 28,
'Lanes': [{'L': 1, 'MF': 7}, {'L': 2, 'MF': 6}]},
{'LM': 602,
'SA/LK': 69,
'RT': 'Strategic Approach Record',
'SG': '3',
'PT': 17,
'Lanes': [{'L': 1, 'MF': 2}]},
{'LM': 602,
'SA/LK': 70,
'RT': 'Strategic Approach Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]},
{'LM': 602,
'SA/LK': 3,
'RT': 'Link Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 4,
'RT': 'Link Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]}],
'SP': [{'A': '(Stretch)33%', 'B': '20%', 'C': '27%', 'D': '20%'}]}
现在这是一本字典,所以如果您只需要 SM
数据数组:
In [3]: json.loads(data)['SM']
Out[3]:
[{'LM': 602,
'SA/LK': 67,
'RT': 'Strategic Approach Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 68,
'RT': 'Strategic Approach Record',
'SG': '2',
'PT': 28,
'Lanes': [{'L': 1, 'MF': 7}, {'L': 2, 'MF': 6}]},
{'LM': 602,
'SA/LK': 69,
'RT': 'Strategic Approach Record',
'SG': '3',
'PT': 17,
'Lanes': [{'L': 1, 'MF': 2}]},
{'LM': 602,
'SA/LK': 70,
'RT': 'Strategic Approach Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]}, {'LM': 602,
'SA/LK': 3,
'RT': 'Link Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 4,
'RT': 'Link Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]}]
我得到了一些文本文件,格式如下:
{"MessageType":"33 (SM data)","REG":"BUL","TS":"2022-02-24T11:02:00","M":false,"LP":3,"CT":119,"SM":[{"LM":602,"SA/LK":67,"RT":"Strategic Approach Record","SG":"1","PT":52,"Lanes":[{"L":1,"MF":15},{"L":2,"MF":19}]},{"LM":602,"SA/LK":68,"RT":"Strategic Approach Record","SG":"2","PT":28,"Lanes":[{"L":1,"MF":7},{"L":2,"MF":6}]},{"LM":602,"SA/LK":69,"RT":"Strategic Approach Record","SG":"3","PT":17,"Lanes":[{"L":1,"MF":2}]},{"LM":602,"SA/LK":70,"RT":"Strategic Approach Record","SG":"4","PT":70,"Lanes":[{"L":1,"MF":12},{"L":2,"MF":4}]},{"LM":602,"SA/LK":3,"RT":"Link Record","SG":"1","PT":52,"Lanes":[{"L":1,"MF":15},{"L":2,"MF":19}]},{"LM":602,"SA/LK":4,"RT":"Link Record","SG":"4","PT":70,"Lanes":[{"L":1,"MF":12},{"L":2,"MF":4}]}],"SP":[{"A":"(Stretch)33%","B":"20%","C":"27%","D":"20%"}]}
有什么方法可以将文本拆分成如下所示的内容:
[{'LM': 602,
'SA/LK': 67,
'RT': 'Strategic Approach Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 68,
'RT': 'Strategic Approach Record',
'SG': '2',
'PT': 28,
'Lanes': [{'L': 1, 'MF': 7}, {'L': 2, 'MF': 6}]},
{'LM': 602,
'SA/LK': 69,
'RT': 'Strategic Approach Record',
'SG': '3',
'PT': 17,
'Lanes': [{'L': 1, 'MF': 2}]},
{'LM': 602,
'SA/LK': 70,
'RT': 'Strategic Approach Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]},
{'LM': 602,
'SA/LK': 3,
'RT': 'Link Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 4,
'RT': 'Link Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]}]
这样我就可以轻松地将其转换为列表或字典...
我试过使用 string.split(','),但实际上不起作用 - 因为它会拆分每个 ','
看起来像 JSON。使用 built-in json
库解析成 Python 对象:
In [1]: import json
In [2]: data = '...' # string provided by OP, read from file
In [3]: json.loads(data)
Out[3]:
{'MessageType': '33 (SM data)',
'REG': 'BUL',
'TS': '2022-02-24T11:02:00',
'M': False,
'LP': 3,
'CT': 119,
'SM': [{'LM': 602,
'SA/LK': 67,
'RT': 'Strategic Approach Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 68,
'RT': 'Strategic Approach Record',
'SG': '2',
'PT': 28,
'Lanes': [{'L': 1, 'MF': 7}, {'L': 2, 'MF': 6}]},
{'LM': 602,
'SA/LK': 69,
'RT': 'Strategic Approach Record',
'SG': '3',
'PT': 17,
'Lanes': [{'L': 1, 'MF': 2}]},
{'LM': 602,
'SA/LK': 70,
'RT': 'Strategic Approach Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]},
{'LM': 602,
'SA/LK': 3,
'RT': 'Link Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 4,
'RT': 'Link Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]}],
'SP': [{'A': '(Stretch)33%', 'B': '20%', 'C': '27%', 'D': '20%'}]}
现在这是一本字典,所以如果您只需要 SM
数据数组:
In [3]: json.loads(data)['SM']
Out[3]:
[{'LM': 602,
'SA/LK': 67,
'RT': 'Strategic Approach Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 68,
'RT': 'Strategic Approach Record',
'SG': '2',
'PT': 28,
'Lanes': [{'L': 1, 'MF': 7}, {'L': 2, 'MF': 6}]},
{'LM': 602,
'SA/LK': 69,
'RT': 'Strategic Approach Record',
'SG': '3',
'PT': 17,
'Lanes': [{'L': 1, 'MF': 2}]},
{'LM': 602,
'SA/LK': 70,
'RT': 'Strategic Approach Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]}, {'LM': 602,
'SA/LK': 3,
'RT': 'Link Record',
'SG': '1',
'PT': 52,
'Lanes': [{'L': 1, 'MF': 15}, {'L': 2, 'MF': 19}]},
{'LM': 602,
'SA/LK': 4,
'RT': 'Link Record',
'SG': '4',
'PT': 70,
'Lanes': [{'L': 1, 'MF': 12}, {'L': 2, 'MF': 4}]}]