Python—将json文件编写为字典列表
我正在根据从url提取的信息编写一个json文件。如何在单独的行上打印词典的每个元素 这是我当前的代码:Python—将json文件编写为字典列表,python,json,Python,Json,我正在根据从url提取的信息编写一个json文件。如何在单独的行上打印词典的每个元素 这是我当前的代码: dct=[{"name": name, "cuisine": cuisine, "price-range": price, "address": address, "rating": rating, "reviews": score, "district": district, "
dct=[{"name": name,
"cuisine": cuisine,
"price-range": price,
"address": address,
"rating": rating,
"reviews": score,
"district": district,
"url": link
}]
with open('openrice_data.json', 'a') as file:
file.write(json.dumps(dct))
例如,它当前的打印方式如下:
[{"cuisine": ["Japanese", "Hot Pot", "Buffet"], "rating": [3.5], "address": [22.3825, 114.1901], "url": ["https://www.openrice.com/en/hongkong/r-wagyu-more-sha-tin-japanese-hot-pot-r172321"], "reviews": [35, 17, 8], "name": "Wagyu More", "price-range": ["$101-200"], "district": ["Sha Tin"]}]
[
{"name": "Chan Kun Kee",
"cuisine": ["Guang Dong", "Dai Pai Dong"],
"price-range": "$51-100",
"address": [22.3884, 114.1958],
"rating": 3.5,
"reviews": [216, 95, 38],
"district": "Shatin",
"url": "www.openrice.com/en/hongkong/r-chan-kun-kee-sha-tin-guangdong-r7918"
}
]
我希望它像这样打印:
[{"cuisine": ["Japanese", "Hot Pot", "Buffet"], "rating": [3.5], "address": [22.3825, 114.1901], "url": ["https://www.openrice.com/en/hongkong/r-wagyu-more-sha-tin-japanese-hot-pot-r172321"], "reviews": [35, 17, 8], "name": "Wagyu More", "price-range": ["$101-200"], "district": ["Sha Tin"]}]
[
{"name": "Chan Kun Kee",
"cuisine": ["Guang Dong", "Dai Pai Dong"],
"price-range": "$51-100",
"address": [22.3884, 114.1958],
"rating": 3.5,
"reviews": [216, 95, 38],
"district": "Shatin",
"url": "www.openrice.com/en/hongkong/r-chan-kun-kee-sha-tin-guangdong-r7918"
}
]
不要使用
json
,pprint
非常适合这份工作
from pprint import pprint
obj = [{"cuisine": ["Japanese", "Hot Pot", "Buffet"], "rating": [3.5], "address": [22.3825, 114.1901], "url": ["https://www.openrice.com/en/hongkong/r-wagyu-more-sha-tin-japanese-hot-pot-r172321"], "reviews": [35, 17, 8], "name": "Wagyu More", "price-range": ["$101-200"], "district": ["Sha Tin"]}]
with open('dumpfile.json', 'w+') as f:
pprint(obj, f)
有几个参数需要定制,详情请查看单据:
不要使用
json
,pprint
非常适合这份工作
from pprint import pprint
obj = [{"cuisine": ["Japanese", "Hot Pot", "Buffet"], "rating": [3.5], "address": [22.3825, 114.1901], "url": ["https://www.openrice.com/en/hongkong/r-wagyu-more-sha-tin-japanese-hot-pot-r172321"], "reviews": [35, 17, 8], "name": "Wagyu More", "price-range": ["$101-200"], "district": ["Sha Tin"]}]
with open('dumpfile.json', 'w+') as f:
pprint(obj, f)
有几个参数需要定制,详情请查看单据:
使用预印机:
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(dct)
另外:您当前正在将dict放入列表中。[]是一个列表{}是python中的dict。
把[{}]放在一个列表中就是把这个单词放在一个列表中。只需卸下[]。使用预打印器:
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(dct)
另外:您当前正在将dict放入列表中。[]是一个列表{}是python中的dict。
把[{}]放在一个列表中就是把这个单词放在一个列表中。只需删除[]。更新实际上您拥有的是一个字典列表。如果要添加更多元素,需要删除字典中的
[]
要解决您的特定问题,请使用indent=0。也可以直接考虑使用JSON.DUP。
import json
l=[]
dct={"name": 'name',
"cuisine": 'cuisine',
"price-range": 'price',
"address": 'address',
"rating": 'rating',
"reviews": 'score',
"district": 'district',
"url": 'link'
}
l.append(dct)
with open('openrice_data.json', 'w') as file:
json.dump(l,file,indent=0)
输出:
[
{
"name": "name",
"cuisine": "cuisine",
"price-range": "price",
"address": "address",
"rating": "rating",
"reviews": "score",
"district": "district",
"url": "link"
}
]
继续
要添加更多元素,需要执行以下操作:
# Load json to list
with open('openrice_data.json') as f:
l = json.load(f)
# A new dict
dct2={"name": 'name',
"cuisine": 'cuisine',
"price-range": 'price',
"address": 'address',
"rating": 'rating',
"reviews": 'score',
"district": 'district',
"url": 'link'
}
# Append new dict
l.append(dct2)
with open('openrice_data.json', 'w') as file:
json.dump(l,file,indent=0)
输出现在包含一个包含2个dict的列表
[
{
"name": "name",
"cuisine": "cuisine",
"price-range": "price",
"address": "address",
"rating": "rating",
"reviews": "score",
"district": "district",
"url": "link"
},
{
"name": "name",
"cuisine": "cuisine",
"price-range": "price",
"address": "address",
"rating": "rating",
"reviews": "score",
"district": "district",
"url": "link"
}
]
更新实际上,您拥有的是一个字典列表。如果要添加更多元素,需要删除字典中的
[]
要解决您的特定问题,请使用indent=0。也可以直接考虑使用JSON.DUP。
import json
l=[]
dct={"name": 'name',
"cuisine": 'cuisine',
"price-range": 'price',
"address": 'address',
"rating": 'rating',
"reviews": 'score',
"district": 'district',
"url": 'link'
}
l.append(dct)
with open('openrice_data.json', 'w') as file:
json.dump(l,file,indent=0)
输出:
[
{
"name": "name",
"cuisine": "cuisine",
"price-range": "price",
"address": "address",
"rating": "rating",
"reviews": "score",
"district": "district",
"url": "link"
}
]
继续
要添加更多元素,需要执行以下操作:
# Load json to list
with open('openrice_data.json') as f:
l = json.load(f)
# A new dict
dct2={"name": 'name',
"cuisine": 'cuisine',
"price-range": 'price',
"address": 'address',
"rating": 'rating',
"reviews": 'score',
"district": 'district',
"url": 'link'
}
# Append new dict
l.append(dct2)
with open('openrice_data.json', 'w') as file:
json.dump(l,file,indent=0)
输出现在包含一个包含2个dict的列表
[
{
"name": "name",
"cuisine": "cuisine",
"price-range": "price",
"address": "address",
"rating": "rating",
"reviews": "score",
"district": "district",
"url": "link"
},
{
"name": "name",
"cuisine": "cuisine",
"price-range": "price",
"address": "address",
"rating": "rating",
"reviews": "score",
"district": "district",
"url": "link"
}
]
其他人已经评论过使用
pprint
,但我想补充一点,pprint
会打印字典中Python值的表示形式。它们并不总是与JSON对应项相同,例如:
>>> from pprint import pprint
>>> d1 = {"value": None}
>>> pprint(d1)
{'value': None}
(这里正确的JSON序列化是{“value”:null}
对于这些类型的值,更好的选择是使用json.dump
或json.dumps
。您可以使用indent
参数进行排序,使其每个元素打印一行。不过请注意,这也会将每个列表元素打印到各自的行中(因此,每个json键不能精确获得一行):
但是,您可以保证至少始终获得正确的JSON。此外,您还可以使用自己的扩展行为。例如,这允许您将Python
datetime
对象序列化为JSON字符串。其他人已经评论过如何使用pprint
,但我想补充一点,pprint
打印Python值在字典中的表示。它们并不总是与JSON对应项相同,例如:
>>> from pprint import pprint
>>> d1 = {"value": None}
>>> pprint(d1)
{'value': None}
(这里正确的JSON序列化是{“value”:null}
对于这些类型的值,更好的选择是使用json.dump
或json.dumps
。您可以使用indent
参数进行排序,使其每个元素打印一行。不过请注意,这也会将每个列表元素打印到各自的行中(因此,每个json键不能精确获得一行):
但是,您可以保证至少始终获得正确的JSON。此外,您还可以使用自己的扩展行为。例如,这允许您将Python
datetime
对象序列化为JSON字符串。抱歉,我没有指定-我正在提取餐厅列表的信息,因此每个词典将包含一个餐厅的信息taurant,所以我确实希望输出是一个字典列表。抱歉,我没有指定-我正在提取餐厅列表的信息,因此每个字典将包含一个餐厅的信息,因此我确实希望输出是一个字典列表。将json.dump
替换为json.dumps
@Loïc为什么我要这样做?json.dump是写入文件的正确命令。这将在单独的行上打印每个值,例如,如果餐厅有多个菜系,则每个菜系位于单独的行上line@Greta你是什么意思?这不是你想要的吗?请看我更新的答案,因为我认为你使用了错误的结构。将json.dump
替换为json.dumps
@Loïc为什么要这样做?Json.dump是写入文件的正确命令。它将在单独的行上打印每个值,例如,如果餐厅有多个菜系,则每个菜系都在单独的行上line@Greta你是什么意思?这不是你想要的吗?请看我更新的答案,因为我认为你使用了错误的结构。谢谢你的答案!谢谢或者你的答案!