JSON漂亮的打印不是';t在Python中工作

JSON漂亮的打印不是';t在Python中工作,python,json,Python,Json,据我所知,这段代码应该可以工作 import json with open('path', 'r') as infile: data = json.load(infile) print(json.dumps(data, indent=4)) 但事实并非如此。我明白了: "b'{\"Markets\":[{\"ID\":2461,\"Name\":\"Who will be elected German chancellor in 2017?\",\"ShortName\":\"

据我所知,这段代码应该可以工作

import json
with open('path', 'r') as infile:
    data = json.load(infile)
    print(json.dumps(data, indent=4))
但事实并非如此。我明白了:

"b'{\"Markets\":[{\"ID\":2461,\"Name\":\"Who will be elected German chancellor in 2017?\",\"ShortName\":\"Who will be elected German chancellor in 2017?\",\"TickerSymbol\":\"CHANCELLOR.GERMANY.2017\",\"Image\":\"https://az620379.vo.msecnd.net/images/Markets/5914fd50-9a60-46d1-ac5f-b36c2c4c9832.png\",\"URL\":\"https://www.predictit.org/Market/2461/Who-will-be-elected-German-chancellor-in-2017\",\"Contracts\":[{\"ID\":3613,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_494e673d-12cc-40c5-ab6a-2976c582303f.png\" ...
然而,如果我去给它相同的数据,我会得到相当大的缩进。所以问题在于代码,而不是数据(尽管我不明白为什么会有这么多反斜杠)

我做错了什么

编辑:样本数据

"b'{\"ID\":2461,\"Name\":\"Who will be elected German chancellor in 2017?\",\"ShortName\":\"Who will be elected German chancellor in 2017?\",\"TickerSymbol\":\"CHANCELLOR.GERMANY.2017\",\"Image\":\"https://az620379.vo.msecnd.net/images/Markets/5914fd50-9a60-46d1-ac5f-b36c2c4c9832.png\",\"URL\":\"https://www.predictit.org/Market/2461/Who-will-be-elected-German-chancellor-in-2017\",\"Contracts\":[{\"ID\":3613,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_494e673d-12cc-40c5-ab6a-2976c582303f.png\",\"URL\":\"https://www.predictit.org/Contract/3613/Will-Angela-Merkel-be-elected-German-chancellor-in-2017\",\"Name\":\"Angela Merkel\",\"LongName\":\"Will Angela Merkel be elected German chancellor in 2017?\",\"ShortName\":\"Merkel\",\"TickerSymbol\":\"MERKEL.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.10,\"BestBuyYesCost\":0.10,\"BestBuyNoCost\":0.93,\"BestSellYesCost\":0.07,\"BestSellNoCost\":0.90,\"LastClosePrice\":0.10},{\"ID\":3614,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_d87c6a74-372d-40b5-95d9-2f994738acf5.png\",\"URL\":\"https://www.predictit.org/Contract/3614/Will-Sigmar-Gabriel-be-elected-German-chancellor-in-2017\",\"Name\":\"Sigmar Gabriel\",\"LongName\":\"Will Sigmar Gabriel be elected German chancellor in 2017?\",\"ShortName\":\"Gabriel\",\"TickerSymbol\":\"GABRIEL.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3615,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_be043f00-72fb-479c-b5ae-37e51154935f.png\",\"URL\":\"https://www.predictit.org/Contract/3615/Will-Frank-Walter-Steinmeier-be-elected-German-chancellor-in-2017\",\"Name\":\"Frank-Walter Steinmeier\",\"LongName\":\"Will Frank-Walter Steinmeier be elected German chancellor in 2017?\",\"ShortName\":\"Steinmeier\",\"TickerSymbol\":\"STEINMEIER.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3616,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_9d6f499c-245d-4cba-a1b1-537d1a41408d.png\",\"URL\":\"https://www.predictit.org/Contract/3616/Will-Martin-Schulz-be-elected-German-chancellor-in-2017\",\"Name\":\"Martin Schulz\",\"LongName\":\"Will Martin Schulz be elected German chancellor in 2017?\",\"ShortName\":\"Schulz\",\"TickerSymbol\":\"SCHULZ.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3617,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_a8e62e71-b5f3-4050-8340-c41204d58e30.png\",\"URL\":\"https://www.predictit.org/Contract/3617/Will-Wolfgang-Sch%C3%A4uble-be-elected-German-chancellor-in-2017\",\"Name\":\"Wolfgang Sch\\xc3\\xa4uble\",\"LongName\":\"Will Wolfgang Sch\\xc3\\xa4uble be elected German chancellor in 2017?\",\"ShortName\":\"Sch\\xc3\\xa4uble\",\"TickerSymbol\":\"SCHAUBLE.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3618,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_a41f6020-3a21-4679-ab04-1ff2c4e90e14.png\",\"URL\":\"https://www.predictit.org/Contract/3618/Will-Frauke-Petry-be-elected-German-chancellor-in-2017\",\"Name\":\"Frauke Petry\",\"LongName\":\"Will Frauke Petry be elected German chancellor in 2017?\",\"ShortName\":\"Petry\",\"TickerSymbol\":\"PETRY.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3619,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_18819f42-5bba-4640-96fb-e62ce4879cd0.png\",\"URL\":\"https://www.predictit.org/Contract/3619/Will-Thomas-de-Maizi%C3%A8re-be-elected-German-chancellor-in-2017\",\"Name\":\"Thomas de Maizi\\xc3\\xa8re\",\"LongName\":\"Will Thomas de Maizi\\xc3\\xa8re be elected German chancellor in 2017?\",\"ShortName\":\"de Maizi\\xc3\\xa8re\",\"TickerSymbol\":\"DEMAIZIERE.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3620,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_deffbe18-669e-45c6-9bb5-62ab61363d49.png\",\"URL\":\"https://www.predictit.org/Contract/3620/Will-Ursula-Von-der-Leyen-be-elected-German-chancellor-in-2017\",\"Name\":\"Ursula Von der Leyen\",\"LongName\":\"Will Ursula Von der Leyen be elected German chancellor in 2017?\",\"ShortName\":\"Von der Leyen\",\"TickerSymbol\":\"VONDERLEYEN.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3621,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_0b630af8-c76c-427c-91af-50a2e5f37bc4.png\",\"URL\":\"https://www.predictit.org/Contract/3621/Will-Christian-Lindner-be-elected-German-chancellor-in-2017\",\"Name\":\"Christian Lindner\",\"LongName\":\"Will Christian Lindner be elected German chancellor in 2017?\",\"ShortName\":\"Lindner\",\"TickerSymbol\":\"LINDNER.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3622,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_e6aaf4d2-00c7-437c-82c0-d29336d75ad7.png\",\"URL\":\"https://www.predictit.org/Contract/3622/Will-Horst-Seehofer-be-elected-German-chancellor-in-2017\",\"Name\":\"Horst Seehofer\",\"LongName\":\"Will Horst Seehofer be elected German chancellor in 2017?\",\"ShortName\":\"Seehofer\",\"TickerSymbol\":\"SEEHOFER.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3623,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_59a0cbcf-8bbf-4f39-ab30-175d9213732f.png\",\"URL\":\"https://www.predictit.org/Contract/3623/Will-Cem-%C3%96zdemir-be-elected-German-chancellor-in-2017\",\"Name\":\"Cem \\xc3\\x96zdemir\",\"LongName\":\"Will Cem \\xc3\\x96zdemir be elected German chancellor in 2017?\",\"ShortName\":\"\\xc3\\x96zdemir\",\"TickerSymbol\":\"OZDEMIR.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3624,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_076c33fc-39fc-414f-abf2-cc7dfa2e4dad.png\",\"URL\":\"https://www.predictit.org/Contract/3624/Will-Katja-Kipping-be-elected-German-chancellor-in-2017\",\"Name\":\"Katja Kipping\",\"LongName\":\"Will Katja Kipping be elected German chancellor in 2017?\",\"ShortName\":\"Kipping\",\"TickerSymbol\":\"KIPPING.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3625,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_84a1659d-63fd-4169-a5e7-49762df694e2.png\",\"URL\":\"https://www.predictit.org/Contract/3625/Will-Hannelore-Kraft-be-elected-German-chancellor-in-2017\",\"Name\":\"Hannelore Kraft\",\"LongName\":\"Will Hannelore Kraft be elected German chancellor in 2017?\",\"ShortName\":\"Kraft\",\"TickerSymbol\":\"KRAFT.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3626,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_80d9880b-1881-4079-9935-c0a5691876e9.png\",\"URL\":\"https://www.predictit.org/Contract/3626/Will-Peter-Altmaier-be-elected-German-chancellor-in-2017\",\"Name\":\"Peter Altmaier\",\"LongName\":\"Will Peter Altmaier be elected German chancellor in 2017?\",\"ShortName\":\"Altmaier\",\"TickerSymbol\":\"ALTMAIER.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":3627,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_cb11286d-c0b9-41fc-a4b0-3f52dccbd9bf.png\",\"URL\":\"https://www.predictit.org/Contract/3627/Will-Olaf-Scholz-be-elected-German-chancellor-in-2017\",\"Name\":\"Olaf Scholz\",\"LongName\":\"Will Olaf Scholz be elected German chancellor in 2017?\",\"ShortName\":\"Scholz\",\"TickerSymbol\":\"SCHOLZ.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":4866,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_72649d7f-0d79-4d34-a061-3920669d69c3.png\",\"URL\":\"https://www.predictit.org/Contract/4866/Will-Jens-Spahn-be-elected-German-chancellor-in-2017\",\"Name\":\"Jens Spahn\",\"LongName\":\"Will Jens Spahn be elected German chancellor in 2017?\",\"ShortName\":\"Spahn\",\"TickerSymbol\":\"SPAHN.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":4885,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_86dc58ff-2801-4174-acb6-3fea5f5b801f.png\",\"URL\":\"https://www.predictit.org/Contract/4885/Will-Leif-Erik-Holm-be-elected-German-chancellor-in-2017\",\"Name\":\"Leif-Erik Holm\",\"LongName\":\"Will Leif-Erik Holm be elected German chancellor in 2017?\",\"ShortName\":\"Holm\",\"TickerSymbol\":\"HOLM.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":4920,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_bc912c09-4d05-4d0a-9bba-d2dda6621b5d.png\",\"URL\":\"https://www.predictit.org/Contract/4920/Will-Karl-Theodor-zu-Guttenberg-be-elected-German-chancellor-in-2017\",\"Name\":\"Karl-Theodor zu Guttenberg\",\"LongName\":\"Will Karl-Theodor zu Guttenberg be elected German chancellor in 2017?\",\"ShortName\":\"Guttenberg\",\"TickerSymbol\":\"GUTTENBERG.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.02,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.98,\"LastClosePrice\":0.01},{\"ID\":7974,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_a2440597-a99e-4ab3-96cc-0c821bb978a0.png\",\"URL\":\"https://www.predictit.org/Contract/7974/Will-Alexander-Gauland-be-elected-German-chancellor-in-2017\",\"Name\":\"Alexander Gauland\",\"LongName\":\"Will Alexander Gauland be elected German chancellor in 2017?\",\"ShortName\":\"Gauland\",\"TickerSymbol\":\"GAUL.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01},{\"ID\":7975,\"DateEnd\":\"N/A\",\"Image\":\"https://az620379.vo.msecnd.net/images/Contracts/small_9bec806a-8b80-47e2-bd03-c279a2b6b799.png\",\"URL\":\"https://www.predictit.org/Contract/7975/Will-Alice-Weidel-be-elected-German-chancellor-in-2017\",\"Name\":\"Alice Weidel\",\"LongName\":\"Will Alice Weidel be elected German chancellor in 2017?\",\"ShortName\":\"Weidel\",\"TickerSymbol\":\"WEID.CHANCELLOR.GERMANY.2017\",\"Status\":\"Open\",\"LastTradePrice\":0.01,\"BestBuyYesCost\":0.01,\"BestBuyNoCost\":null,\"BestSellYesCost\":null,\"BestSellNoCost\":0.99,\"LastClosePrice\":0.01}],\"TimeStamp\":\"2017-12-09T01:44:29.7878389\",\"Status\":\"Open\"}'"
编辑2:生成json文件的公认的混乱代码

from urllib.request import Request, urlopen

q = Request('https://www.predictit.org/api/marketdata/all/')
q.add_header('Accept', 'application/json')

import json
import datetime

def saveData(data):
    now = datetime.datetime.now()
    path = 'json/PI ' + str(now) + '.json'
    path = path.replace(':','-') # No colons in file name
    with open(path, 'w') as outfile:
        json.dump(str(data), outfile, ensure_ascii=False)

import time
starttime=time.time()
while True:
    data = urlopen(q).read()
    saveData(data)
    time.sleep(60.0 - ((time.time() - starttime) % 60.0))

您是如何生成此文件的?它不是一个JSON对象,而是一个JSON字符串,包含一个Python repr,其中包含一个原始JSON字符串。或者类似的。由于所有额外的反斜杠和前导的
b'
字符,您可以看出这一点

这项工作:

import json
from ast import literal_eval

file_data = open('test_file.json', 'r').read()
python_repr = json.loads(file_data)
json_string = literal_eval(python_repr)
actual_dict = json.loads(json_string.decode('utf-8'))
print(json.dumps(actual_dict, indent=4)) # finally.
但你一开始是怎么做到的

您也可以将其写回简单JSON,而不是多重编码的wtf:

with open('output.json', 'wb') as out_file:
    json.dump(actual_dict, out_file, indent=4)
编辑以进行文件写入:

当您从网站读取数据时,它是一个包含JSON对象的字符串。您需要首先将其转换为Python
dict
,然后将其写出没有再次调用
str
。这就是为什么你会得到三重编码的混乱。使用上面的代码:

data = urlopen(q).read()
json_data = json.loads(data.decode('utf-8')) # DECODE THE STRING 
with open('output.json', 'w') as out:
    json.dump(json_data, out, indent=4) # DO NOT CALL STR ON IT AGAIN
写得干净利落后,您也可以轻松阅读:

json_loaded = json.load(open('output.json', 'r'))
这将以python dict的形式加载它

我建议您进一步了解数据本身与数据的各种表示之间的差异

EDIT2:还要注意,如果您只想保存数据,只需将从网站获取的内容直接写入文件,而无需使用
json.load/dump
roundtrip

data = urlopen(q).read()
with open('output.json', 'w') as out:
    out.write(data)

你能发布整个数据吗?这是639KB的数据。给你:也许你能拿出一些示例数据,我们可以用这些数据重现这个问题,这个数据小于639KB?是的,正是@ChrisMartin saidI从网站的API中得到的。参见编辑2。但是,是的,最终源数据应该是正确的。不确定我是如何错误地存储它的。也许我可以在另一篇博文中问一下。@nimbus7你是从一个字符串开始的,而不是把它解码成一个dict,然后再调用
str
,这就是你最终陷入混乱的原因。请参阅我的编辑,以获得一种干净的方式来编写您获得的数据。我是python新手,因此我犯了这个错误并不感到惊讶。谢谢。@nimbus7不客气!另请参阅我的第二次编辑-您可能只需要将API中的数据直接保存到文件中,不需要在循环中实际解析JSON,除非您还需要对其执行其他操作。它将正确加载。