Python-将json文件写为字典列表

时间:2017-11-13 08:04:25

标签: python json

我正在从网址中提取的信息中编写一个json文件。如何在单独的行上打印字典的每个元素?

这是我目前的代码:

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"
}
]

4 个答案:

答案 0 :(得分:3)

不要使用jsonpprint非常适合这项工作。

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)

有一些自定义参数,请查看文档了解更多详情: https://docs.python.org/3/library/pprint.html

答案 1 :(得分:3)

更新实际上您拥有的是词典列表。如果要添加更多元素,则需要删除字典周围的[]

要解决您想要使用的特定问题,请使用indent = 0。另外考虑直接使用json.dump。

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个dicts的列表。

[
{
"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"
}
]

答案 2 :(得分:2)

使用prettyprinter:

import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(dct)

另外:您目前正在将dict放入列表中。 []是一个列表{}是python中的一个字典。 通过输入[{}],您将dict放入列表中。只需删除[]。

答案 3 :(得分:-1)

其他人已经评论过使用pprint,但我想补充说pprint在字典中打印Python值的表示。它们并不总是与JSON对应物相同,例如:

>>> from pprint import pprint
>>> d1 = {"value": None}
>>> pprint(d1)
{'value': None}

(这里正确的JSON序列化是{"value": null}

对于这些类型的值,更好的选择是使用json.dumpjson.dumps。您可以使用indent参数对其进行排序,使其每个元素打印一行。请注意,这也会将每个列表元素打印到单独的行中(因此您不必每个JSON键一行):

>>> d2 = [
... {"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"
... }
... ]
>>> print(json.dumps(d2, indent=2))
[
  {
    "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。此外,您还可以使用自己的JSON encoder扩展行为。例如,这允许您将Python datetime对象序列化为JSON字符串。