Extend list to json file in Python

Viewed 489

I've got a array saved in a json file looking like this [4.810,-75.700,0.020,11,5.070,-75.520,0.010,11]. I'm using Python to append new 4-tuples to this array.

globe_list = [18.110,-66.170,0.000,11]
json_array = json.dumps(globe_list)
    with open(webgl_file_path + 'tweet_locations.json', 'a') as tf:
        tf.write(json_array)

The problem is, when the file already exists, what I get after the appending is two arrays:

[4.810,-75.700,0.020,11,5.070,-75.520,0.010,11][18.110,-66.170,0.000,11]

Whereas what I want is one array:

[4.810,-75.700,0.020,11,5.070,-75.520,0.010,11, 18.110,-66.170,0.000,11]

If I would load the json array in a list first a could just extend it, but the file is huge and I'm worrying performance issues. Is there a simple way to do this?

Thanks in advance.

2 Answers

You could try moving the file pointer to the position of the final "]", and then writing the additional json, without the initial "[", like this:

>>> import io
>>> with open('example.json', 'rb+') as f:
...     f.seek(-1, io.SEEK_END)
...     f.write(b', ' + new_json[1:].encode())

Note that this must be done with the file in binary mode. The above code assumes that the filesystem encoding encodes "]" as a single byte, and that there isn't a newline character(s) and the end of the file. If either of these conditions hold you would need to adjust the offset passed to seek.

You could read the file as a string, remove the last ']', convert globe_list to string, remove its first '[' and write back the appended string.

f = open(webgl_file_path + 'tweet_locations.json')
cont = f.read()
del f[len(f) - 1]
jsonstr = str(globe_list)[1:]
final_cont = cont + jsonstr
close(f)
w = open(webgl_file_path + 'tweet_locations.json', 'w')
w.write(final_cont)

I suppose this won't be very fast, but it would be definitely more performant than using json.load / json.dump and extending the list, since this involves only string read/write and manipulation.

Related