I have a jsonline file like below:
{"id":0,"country":"fr"}
{"id":1,"country":"en"}
{"id":2,"country":"fr"}
{"id":3,"country":"fr"}
I have a list of codes, i want to attribute a code to each user, by updating the file lines.
The result should be the following:
{"id":0,"country":"fr", code:1}
{"id":1,"country":"en", code:2}
{"id":2,"country":"fr", code:3}
{"id":3,"country":"fr", code:4}
This is how i do it now:
import ujson
fh, abs_path = mkstemp()
with open(fh, 'w') as tmp_file:
with open(shooting.segment_filename) as segment_filename:
for line in segment_filename:
enriched_line = ujson.loads(line)
code = compute_code()
if code:
enriched_line["code"] = code
tmp_file.write(ujson.dumps(enriched_line) + '\n')
My question is, is there a faster way to do this ? May be via a linux command launched via sarge for example ? or any pythonic way without having to read the read / write / replace the original file ?
Thank you !