I am receiving data over a socket, a bunch of JSON strings. However, I receive a set amount of bytes, so sometimes the last of my JSON strings is cut-off. I will typically g
Extracting lines that start and end with a specific character can be done without any regex, use str.startswith
and str.endswith
methods when iterating through the lines in a file:
results = []
with open(filepath, 'r') as f:
for line in f:
if line.startswith('{') and line.rstrip('\n').endswith('}'):
results.append(line.rstrip('\n'))
Note the .rstrip('\n')
is used before .endswith
to make sure the final newline does not interfere with the }
check at the end of the string.