【问题标题】:How to update key's value in json file by Python如何通过 Python 更新 json 文件中的键值
【发布时间】:2022-01-06 16:57:18
【问题描述】:

我想从不同的页面收集数据,但是当我获取数据并附加到 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"
    ]
  ]
}```

【问题讨论】:

    标签: python json python-3.x parsing request


    【解决方案1】:

    问题来了:

    data["num"]+=([num_list])
    

    您假设num_list 不是一个数组,而您当前正在将它包装在另一个数组中。因此,您添加的不是值列表,而是另一个数组到您当前存储在 json 数据中。

    要解决这个问题,只需将代码更改为:

    data["num"]+=num_list
    

    除此之外,您正在以附加模式写入,并且您正在更新内存上的 json 数据,然后再写入磁盘。把模式改成这样写:

    # change
    f = open("data.json", "a")
    # to
    f = open("data.json", "w")
    

    关于您共享的输出,一开始有一种奇怪的格式。对吗?

    【讨论】:

    • 我认为你是对的,你认为修复如何?
    • 去掉括号即可。你在那里不需要它们。
    • 我做了但结果相同
    • 不抱歉,不一样但又错了。你能看到吗? pastebin.com/6YueAyP5
    • 哦!现在我了解了您最初共享的输出的开始部分的含义...发生这种情况是因为您使用 append 作为文件模式,因此当您编写它时,会将新数组附加到文件的末尾。
    猜你喜欢
    • 2021-10-04
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2012-05-27
    • 1970-01-01
    相关资源
    最近更新 更多