如何通过 Python 更新 json 文件中的键值
How to update key's value in json file by Python
我想从不同的页面收集数据,但是当我获取数据并附加到 json 文件时,它在结构上是错误的。我有一个字典 num
,我想将所有数据附加到列表中。例如
num:[1,2,3]
添加后
num:[1,2,3,4,5]
这是我的代码 -->
import requests
import json
from random import randint
from time import sleep
import os
num_list=[]
def req(i):
url = 'Website_link{}'.format(i)
x = requests.get(url, headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "ka",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/json",
"Host": "api2.myauto.ge",
"Origin": "https://www.myauto.ge",
"Pragma": "no-cache",
"Referer": "https://www.myauto.ge/",
"sec-ch-ua": "'Not A;Brand';v='99', 'Chromium';v='96', 'Google Chrome';v='96'",
"sec-ch-ua-platform": "Windows",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site"})
dict_to_json = json.loads(x.content.decode("utf-8"))
pretty_json = json.dumps(dict_to_json, indent=4)
return dict_to_json
def list_append(parse):
for i in parse["data"]["items"]:
num_list.append(str(i["client_phone"]))
def write_to_json():
num_dict={"num":num_list}
if os.stat("data.json").st_size == 0:
f = open("data.json", "w")
json.dump(num_dict, f, indent=2)
f.close()
else:
f = open("data.json","r+")
data=json.load(f)
f.close()
f = open("data.json", "a")
data["num"]+=([num_list])
json.dump(data,f,indent=2)
f.close()
for i in range(1,3):
sleep(randint(3, 5))
req_load=req(i)
list_append(req_load)
write_to_json()
num_list=[]
我得到这样的数据
"num": [
"995579448820",
"995595334438",
"995555154416",
"995577025245",
"995599485252",
"995597083412",
"995598919090",
"995599401516",
"995597092860",
"995551317007",
"995577063439",
"995514213366",
"995592088897",
"995577539231",
"995596940606"
]
}{
"num": [
"995579448820",
"995595334438",
"995555154416",
"995577025245",
"995599485252",
"995597083412",
"995598919090",
"995599401516",
"995597092860",
"995551317007",
"995577063439",
"995514213366",
"995592088897",
"995577539231",
"995596940606",
[
"995597032700",
"995595201008",
"995593620467",
"995555605884",
"995555183888",
"995597777422",
"995577125074",
"995595910006",
"995595910006",
"995577583288",
"995599037070",
"995558830303",
"995591085255",
"995597777608",
"995555788888"
]
]
}```
这是问题所在:
data["num"]+=([num_list])
您假设 num_list
不是数组,并且您当前将其包装在另一个数组中。因此,您添加的不是值列表,而是另一个数组到您当前存储在 json 数据中。
要解决这个问题,只需将您的代码更改为:
data["num"]+=num_list
除此之外,您正在以追加模式写入,并在写入磁盘之前更新内存中的 json 数据。改模式写成这样:
# change
f = open("data.json", "a")
# to
f = open("data.json", "w")
关于您分享的输出,开头的格式很奇怪。是吗?
我想从不同的页面收集数据,但是当我获取数据并附加到 json 文件时,它在结构上是错误的。我有一个字典 num
,我想将所有数据附加到列表中。例如
num:[1,2,3]
添加后
num:[1,2,3,4,5]
这是我的代码 -->
import requests
import json
from random import randint
from time import sleep
import os
num_list=[]
def req(i):
url = 'Website_link{}'.format(i)
x = requests.get(url, headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "ka",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Type": "application/json",
"Host": "api2.myauto.ge",
"Origin": "https://www.myauto.ge",
"Pragma": "no-cache",
"Referer": "https://www.myauto.ge/",
"sec-ch-ua": "'Not A;Brand';v='99', 'Chromium';v='96', 'Google Chrome';v='96'",
"sec-ch-ua-platform": "Windows",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site"})
dict_to_json = json.loads(x.content.decode("utf-8"))
pretty_json = json.dumps(dict_to_json, indent=4)
return dict_to_json
def list_append(parse):
for i in parse["data"]["items"]:
num_list.append(str(i["client_phone"]))
def write_to_json():
num_dict={"num":num_list}
if os.stat("data.json").st_size == 0:
f = open("data.json", "w")
json.dump(num_dict, f, indent=2)
f.close()
else:
f = open("data.json","r+")
data=json.load(f)
f.close()
f = open("data.json", "a")
data["num"]+=([num_list])
json.dump(data,f,indent=2)
f.close()
for i in range(1,3):
sleep(randint(3, 5))
req_load=req(i)
list_append(req_load)
write_to_json()
num_list=[]
我得到这样的数据
"num": [
"995579448820",
"995595334438",
"995555154416",
"995577025245",
"995599485252",
"995597083412",
"995598919090",
"995599401516",
"995597092860",
"995551317007",
"995577063439",
"995514213366",
"995592088897",
"995577539231",
"995596940606"
]
}{
"num": [
"995579448820",
"995595334438",
"995555154416",
"995577025245",
"995599485252",
"995597083412",
"995598919090",
"995599401516",
"995597092860",
"995551317007",
"995577063439",
"995514213366",
"995592088897",
"995577539231",
"995596940606",
[
"995597032700",
"995595201008",
"995593620467",
"995555605884",
"995555183888",
"995597777422",
"995577125074",
"995595910006",
"995595910006",
"995577583288",
"995599037070",
"995558830303",
"995591085255",
"995597777608",
"995555788888"
]
]
}```
这是问题所在:
data["num"]+=([num_list])
您假设 num_list
不是数组,并且您当前将其包装在另一个数组中。因此,您添加的不是值列表,而是另一个数组到您当前存储在 json 数据中。
要解决这个问题,只需将您的代码更改为:
data["num"]+=num_list
除此之外,您正在以追加模式写入,并在写入磁盘之前更新内存中的 json 数据。改模式写成这样:
# change
f = open("data.json", "a")
# to
f = open("data.json", "w")
关于您分享的输出,开头的格式很奇怪。是吗?