2

I have string in the format of:

'{comm:comm123,date: Aug 29, 2019 12:30:00 PM,value:qwert}'

I want to get something like this:

{
   'comm': 'comm123',
   'date': Aug 29, 2019 12:30:00 PM,   (As a date)
   'value': 'qwert'
}

I have tried using literal_eval and eval but they do not help. i also tried using json library but it does not seem to serve any purpose.

utkj97
  • 21
  • 2

2 Answers2

0

Your string is indeed ambiguous, however, you can use re.findall:

import re
d = '{comm:comm123,date: Aug 29, 2019 12:30:00 PM,value:qwert}'
new_d = re.findall('[A-Z][a-z]{2}\s\d{2},\s\d{4}\s\d{2}:\d{2}:\d{2} [APM]{2}|\w+', d)
result = {new_d[i]:new_d[i+1] for i in range(0, len(new_d), 2)}

Output:

{'comm': 'comm123', 'date': 'Aug 29, 2019 12:30:00 PM', 'value': 'qwert'}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
-1

Horrible. The keys seem like they should be unambiguous though - if you know they are always alpha-only could extract them using something like ([a-zA-Z]+):, then use that to get the "other" bits of the string which must be the values?

lost
  • 2,210
  • 2
  • 20
  • 34