gpt4 book ai didi

python - 如何使用python将数据转储到Json文件中

转载 作者:行者123 更新时间:2023-12-05 05:42:08 26 4
gpt4 key购买 nike

如何将数据转储到Json文件中*正如在下面的 python 代码中看到的那样,我正在尝试在 Json 文件中转储数据,但我正在努力在 python 代码中做到这一点 *

import time
import json
import os


def long_function(name):
cache_path = 'cache.json'
if not os.path.isfile(cache_path):
with open(cache_path, 't') as json_file:
cache_file_data = [name]
jsondump(cache_file_data, json_file)
else:
with open(cache_path, 'r') as json_file:
cache_file_data = json.load(json_file)

if name in cache_file_data:
print("Name already exist")
return name
else:
cache_file_data.append(name)
for e in range(5):
time.sleep(1)
print(e+1)
with open(cache_path, 'w') as json_file:
jsondump(cache_file_data, json_file)
print("New Name added in cache")
return name


print(long_function('nitu'))

所以请解决我的问题......请帮助我

最佳答案

import json

# JSON data:
x = '{ "organization":"New_holn",
"city":"Noida",
"country":"India"}'

# python object to be appended
y = {"pin":117845}

# parsing JSON string:
z = json.loads(x)

# appending the data
z.update(y)

# the result is a JSON string:
print(json.dumps(z))

关于python - 如何使用python将数据转储到Json文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72122244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com