【问题标题】:Reading JSON file with Python 3使用 Python 3 读取 JSON 文件
【发布时间】:2016-12-03 07:47:36
【问题描述】:

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

我正在尝试使用json 模块解析这个JSON 文件。如docs 中所述,JSON 文件必须符合RFC 7159。我检查了我的文件here,它告诉我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)

我遇到了这个异常:

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)

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

【问题讨论】:

  • 文件的编码有问题。对于它的价值,requests.get("http://pastebin.com/raw/Yjs6FAfm").json() 有效:)
  • @cricket_007 因为文件是UTF-8-BOM 而不是UTF-8,所以我不得不使用encoding='utf-8-sig' 来避免文件开头出现奇怪的字符,这是可能的
  • 对,问题出在文件开头,line 1 column 1 很明显
  • @cricket_007 是的,@Will Molter 的答案有效,读取文本内容而不是文件,也许json#load 忽略了文件编码?
  • 不知道,我以为它只是在文件上做了.read()

标签: python json parsing


【解决方案1】:

试试这个

import json

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

print (array)

【讨论】:

  • 我还是一样的异常,'r'是默认打开方式
  • @Andres 我猜问题出在您的本地文件上。再次检查。
  • 你保存文件的时候用的是什么编码,大概是
  • @Andres 我把它保存为 UTF-8
  • 嘿@Andres,很抱歉挖掘了一个旧线程但是你如何在Windows机器上保存utf-8中的.json文件?我在Json Dumps 函数中看不到选项。我在 Windows 机器上创建了一个 token.json。并且当它被部署到 GCP 时,它给 utf8 的起始字节编码错误,这当然使用云上的 ubuntu 机器。任何建议表示赞赏:)
【解决方案2】:

基于再次阅读documentation,看来您需要将第三行更改为

json_data = json.loads(text)

或删除该行

text = json_file.read()

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

【讨论】:

  • 这个可行,但为什么我不能直接打开文件?根据这个docs.python.org/3/library/json.html#json.load我应该可以。
  • 啊,我想通了;如果你先调用read(),那么文件对象的索引在文件的末尾,不再有任何东西可以用来制作json。
【解决方案3】:

对于 python3,只有以下对我有用 - 以上都不是。

导入 json

with open('/emp.json', 'r') as f:

 data=f.read()

打印(数据)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多