Piyush Divyanakar
Piyush Divyanakar

Reputation: 241

Fix a malformed string?

I have data coming from a TCP line as list of dictionaries. But sometimes I receive two packets at once. They look like

[{"property1":"value1", "property2":"value2"}, {"property1":"value1", "property2":"value2"}][{"property1":"value1", "property2":"value2"}][{"property1":"value1", "property2":"value2"}, {"property1":"value1", "property2":"value2"}]

I want to convert it into this:-

[{"property1":"value1", "property2":"value2"}, {"property1":"value1", "property2":"value2"}, {"property1":"value1", "property2":"value2"},
 {"property1":"value1", "property2":"value2"}, {"property1":"value1", "property2":"value2"}]

Upvotes: 1

Views: 576

Answers (2)

Van Peer
Van Peer

Reputation: 2167

Input

l='[{"property1":"value1", "property2":"value2"}, {"property1":"value1", "property2":"value2"}][{"property1":"value1", "property2":"value2"}][{"property1":"value1", "property2":"value2"}, {"property1":"value1", "property2":"value2"}]'

Using re

import re
re.findall("\[?([^\]]+)?\]", l)

Output

['{"property1":"value1", "property2":"value2"}, {"property1":"value1", "property2":"value2"}',
 '{"property1":"value1", "property2":"value2"}', 
 '{"property1":"value1", "property2":"value2"}, {"property1":"value1", "property2":"value2"}'
]

Upvotes: 1

cs95
cs95

Reputation: 403012

You can do this in 2 steps:

  1. Replace ][ with a , because those consecutive square brackets represent separate packets, and the easiest way to join them is to replace with a comma
  2. Convert the string to a dictionary with ast.literal_eval, the "safe" eval.

import ast
ast.literal_eval(string.replace('][', ','))

[{'property1': 'value1', 'property2': 'value2'},
 {'property1': 'value1', 'property2': 'value2'},
 {'property1': 'value1', 'property2': 'value2'},
 {'property1': 'value1', 'property2': 'value2'},
 {'property1': 'value1', 'property2': 'value2'}]

The assumption here is that you're receiving these "packets" as strings.

Upvotes: 6

Related Questions