Home > Software engineering >  read text as comment instead of string
read text as comment instead of string

Time:02-24

i got some text file, the format looks like this:

{"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%"}]}

is there any way I can split the text into something looks like this:

[{'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}]}]

so that I can easily convert it into list or dict...

I tried use string.split(','), but doesn't really work - as it will split every ','

CodePudding user response:

Looks like JSON. Use the built-in json library to parse into a Python object:

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%'}]}

This is a dictionary now, so if you want just the SM data array:

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}]}]
  • Related