Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend list to json file in Python

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.

like image 328
Kaschi14 Avatar asked Jul 09 '26 16:07

Kaschi14


1 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.

like image 168
snakecharmerb Avatar answered Jul 12 '26 11:07

snakecharmerb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!