小编Kas*_*i14的帖子

将列表扩展到 Python 中的 json 文件

我有一个保存在 json 文件中的数组,如下所示[4.810,-75.700,0.020,11,5.070,-75.520,0.010,11]。我正在使用 Python 将新的 4 元组附加到这个数组。

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)
Run Code Online (Sandbox Code Playgroud)

问题是,当文件已经存在时,追加后我得到的是两个数组:

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

而我想要的是一个数组:

[4.810,-75.700,0.020,11,5.070,-75.520,0.010,11, 18.110,-66.170,0.000,11]
Run Code Online (Sandbox Code Playgroud)

如果我首先将 json 数组加载到列表中,则可以扩展它,但文件很大,我担心性能问题。有没有一种简单的方法可以做到这一点?

提前致谢。

python json file extend python-3.x

3
推荐指数
1
解决办法
247
查看次数

标签 统计

extend ×1

file ×1

json ×1

python ×1

python-3.x ×1