简体   繁体   English

如何从嵌套的 Json 中提取列表到 CSV?

[英]How to extract list from nested Json into a CSV?

I make use of an API which outputs me adresses.我使用 API 输出我的地址。 However the adresses are nested as such:然而,地址是这样嵌套的:

{
   "totalItemCount":55,
   "pageCount":1,
   "size":100,
   "_links":{
      "self":{
         "href":"\/bag?filters[postcode]=1011PL&ovio-api-key=KEY"
      }
   },
   "_embedded":{
      "adres":[
         {
            "huisnummer":"7",
            "huisletter":"",
            "postcode":"1011PL",
            "huisnummertoevoeging":"",
            "openbareruimte":"Nieuwe Amstelstraat",
            "slug":"1011pl-nieuwe-amstelstraat-7",
            "woonplaats":"Amsterdam",
            "_links":{
               "self":{
                  "href":"\/bag\/1011pl-nieuwe-amstelstraat-7"
               }
            }
         },
         {
            "huisnummer":"25",
            "huisletter":"",
            "postcode":"1011PL",
            "huisnummertoevoeging":"",
            "openbareruimte":"Nieuwe Amstelstraat",
            "slug":"1011pl-nieuwe-amstelstraat-25",
            "woonplaats":"Amsterdam",
            "_links":{
               "self":{
                  "href":"\/bag\/1011pl-nieuwe-amstelstraat-25"
               }
            }
         },

My current script:我当前的脚本:

## Convert Output JSON to CSV
f = open("output.json", "r+")
x = json.loads(f.read())
f.close()
# print(x['_embedded']['adres'][0]['openbareruimte'])

f = csv.writer(open("test.csv", "w"))
f.writerow(["straat","huisnummer","postcode","stad"])
for y in x:
    f.writerow([x["_embedded"]["adres"][0]["openbareruimte"],
                x["_embedded"]["adres"][0]["huisnummer"],
                x["_embedded"]["adres"][0]["postcode"],
                x["_embedded"]["adres"][0]["woonplaats"]])

I want to output all of the streets, numbers, postal codes and cities to CSV, but it only outputs the first adress.我想将 output 的所有街道、号码、邮政编码和城市到 CSV,但它只输出第一个地址。 I have tried using split and format but I'm too unfamiliar with that.我曾尝试使用拆分和格式,但我对此太不熟悉了。 If anyone knows how to make use of the nested data, it would be appreciated.如果有人知道如何使用嵌套数据,将不胜感激。 I could not find any tutorial in regards.我找不到任何有关的教程。

You want to loop over items in x["_embedded"]["adres"] and write items for each y您想遍历x["_embedded"]["adres"]中的项目并为每个y编写项目

for y in x["_embedded"]["adres"]:
    f.writerow(y["openbareruimte"],
               y["huisnummer"],
               y["postcode"],
               y["woonplaats"])

First, x seems to be a dictionary.首先, x似乎是一本字典。 So doing for y in x: ... will iterate over the keys.所以for y in x: ...将遍历键。 In this case it seems to be "totalItemsCount", "pageCount" etc. That's obviously not what you want because you aren't even using y .在这种情况下,它似乎是“totalItemsCount”、“pageCount”等。这显然不是你想要的,因为你甚至没有使用y

The embedded field, as you've used it yourself, is x["_embedded"]["adres"] .嵌入字段,正如您自己使用的那样,是x["_embedded"]["adres"] As you have identified, it's an array of addresses.正如您所确定的,它是一个地址数组。 All you need to is go over it:您只需要 go 就可以了:

addresses = x["_embedded"]["adres"]
for address in addresses:
    f.writerow([address["openbareruimte"],
        address["huisnummer"],
        address["postcode"],
        address["woonplaats"]])

Few more comments about your code:关于您的代码的更多评论:

  • When opening a file, you should always use it as a context manager so it will be closed: with open(...) as f: ... (the reason is that if an exception is raised during json loading, the file isn't closed properly).打开文件时,应始终将其用作上下文管理器,以便将其关闭: with open(...) as f: ... (原因是如果在 json 加载期间引发异常,则该文件是'没有正确关闭)。
  • json can load directly from a file: json.load(f) json可以直接从文件加载: json.load(f)

Considering the above two comments, the correct way to load the json is:考虑到以上两条评论,正确加载json的方法是:

with open("output.json", "r+") as f:
    x = json.load(f)
# no need to call "f.close()"

with open("test.csv", "w") as f:
    writer = csv.writer(f)
    writer.writerow(["straat","huisnummer","postcode","stad"])
    addresses = x["_embedded"]["adres"]
    for address in addresses:
        f.writerow([address["openbareruimte"],
            address["huisnummer"],
            address["postcode"],
            address["woonplaats"]])

You need to iterate through the list in the loaded JSON data.您需要遍历加载的 JSON 数据中的列表。

## Convert Output JSON to CSV

import csv, json

with open("output.json", "r") as f:
    x = json.load(f)

with open("subtract_test.csv", "w", newline="") as outp:
    f = csv.writer(outp)
    f.writerow(["straat","huisnummer","postcode","stad"]) # Header.

    for adres in x["_embedded"]["adres"]:
        f.writerow([adres["openbareruimte"],
                    adres["huisnummer"],
                    adres["postcode"],
                    adres["woonplaats"]])

print("Done")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM