使用 Python 3 读取 JSON 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38644480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:15:17  来源:igfitidea点击:

Reading JSON file with Python 3

pythonjsonparsing

提问by Andres

I'm using Python 3.5.2 on Windows 10 x64. The JSONfile I'm reading is thiswhich is a JSONarray containing 2 more arrays.

我在 Windows 10 x64 上使用 Python 3.5.2。JSON我正在阅读的文件是这个,它是一个JSON包含另外 2 个数组的数组。

I'm trying to parse this JSONfile using the jsonmodule. As described in the docsthe JSONfile must be compliant to RFC 7159. I checked my file hereand it tells me it's perfectly fine with the RFC 7159format, but when trying to read it using this simple python code:

我正在尝试JSON使用json模块解析此文件。如文档中所述,该JSON文件必须符合RFC 7159. 我在这里检查了我的文件,它告诉我RFC 7159格式完全没问题,但是当尝试使用这个简单的 python 代码读取它时:

with open(absolute_json_file_path, encoding='utf-8-sig') as json_file:
    text = json_file.read()
    json_data = json.load(json_file)
    print(json_data)

I'm getting this exception:

我收到此异常:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py", line 2217, in <module>
    globals = debugger.run(setup['file'], None, None)
  File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py", line 1643, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc) 
  File "C:/Users/Andres Torti/Git-Repos/MCF/Sur3D.App/shapes-json-checker.py", line 14, in <module>
    json_data = json.load(json_file)
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I can read this exact file perfectly fine on Javascript but I can't get Python to parse it. Is there anything wrong with my file or is any problem with the Python parser?

我可以在 Javascript 上完全正确地读取这个确切的文件,但我无法让 Python 解析它。我的文件有问题还是 Python 解析器有问题?

采纳答案by Will Molter

Based on reading over the documentationagain, it appears you need to either change the third line to

根据再次阅读文档,您似乎需要将第三行更改为

json_data = json.loads(text)

or remove the line

或删除该行

text = json_file.read()

since read()causes the file's index to reach the end of the file. (I suppose, alternatively, you can reset the index of the file).

因为read()导致文件的索引到达文件的末尾。(我想,或者,您可以重置文件的索引)。

回答by lcastillov

Try this

尝试这个

import json

with open('filename.txt', 'r') as f:
    array = json.load(f)

print (array)