[英]How to extract list from nested Json into a CSV?
我使用 API 输出我的地址。 然而,地址是这样嵌套的:
{
"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"
}
}
},
我当前的脚本:
## 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"]])
我想将 output 的所有街道、号码、邮政编码和城市到 CSV,但它只输出第一个地址。 我曾尝试使用拆分和格式,但我对此太不熟悉了。 如果有人知道如何使用嵌套数据,将不胜感激。 我找不到任何有关的教程。
您想遍历x["_embedded"]["adres"]
中的项目并为每个y
编写项目
for y in x["_embedded"]["adres"]:
f.writerow(y["openbareruimte"],
y["huisnummer"],
y["postcode"],
y["woonplaats"])
首先, x
似乎是一本字典。 所以for y in x: ...
将遍历键。 在这种情况下,它似乎是“totalItemsCount”、“pageCount”等。这显然不是你想要的,因为你甚至没有使用y
。
嵌入字段,正如您自己使用的那样,是x["_embedded"]["adres"]
。 正如您所确定的,它是一个地址数组。 您只需要 go 就可以了:
addresses = x["_embedded"]["adres"]
for address in addresses:
f.writerow([address["openbareruimte"],
address["huisnummer"],
address["postcode"],
address["woonplaats"]])
关于您的代码的更多评论:
with open(...) as f: ...
(原因是如果在 json 加载期间引发异常,则该文件是'没有正确关闭)。json
可以直接从文件加载: json.load(f)
考虑到以上两条评论,正确加载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"]])
您需要遍历加载的 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.