Reputation: 241
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
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
Reputation: 403012
You can do this in 2 steps:
][
with a ,
because those consecutive square brackets represent separate packets, and the easiest way to join them is to replace with a commaast.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