据我所知,此代码应该有效。
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\" ...
但是,如果我转到http://jsonprettyprint.com/并给它相同的数据,我会得到很好的缩进。所以问题在于代码,而不是数据(虽然我不明白为什么会有这么多反斜杠)。
我做错了什么?
编辑:示例数据
"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))
答案 0 :(得分:2)
你是如何制作这个文件的?它不是JSON对象,它是包含 Python repr 的 JSON字符串,其中包含原始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
往返。
data = urlopen(q).read()
with open('output.json', 'w') as out:
out.write(data)