【问题标题】:JSONDecodeError: Expecting value: line 1 column 1 (char 0) at line file_data = json.loads(file_s)JSONDecodeError:预期值:第 1 行第 1 列(字符 0)在第 file_data = json.loads(file_s) 行
【发布时间】:2019-05-02 18:19:02
【问题描述】:

大家好,我正在尝试将一堆 json 文件加载到我的程序中,但每当我尝试运行此代码时:

import json
import os
import sys
import requests

path = r'C:\Users\'
json_files = [pos_json for pos_json in os.listdir(path) if pos_json.endswith('.json')]
print(json_files)
JsonDictionary = {}

struct = {}


try:
    for x in range(len(json_files)):
        Jpath = str(path +'\\'+ json_files[x])
        file = open(Jpath)
        file_s = file.read()
        print("File Read: " + str(file_s))
        file_data = json.loads(file_s) 
    print(Jpath)
    JsonDictionary = json.load(Jpath)

except IOError as e:
    print (e)
    print ('IOError: Unable to open json file. Terminating execution.')
    exit(1)

print (JsonDictionary)

这是错误输出:

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-80-0333b738997d> in <module>
     24 #          print (sys.exc_info())
     25         print("File Read: " + str(file_s))
---> 26         file_data = json.loads(file_s)
     27     print(Jpath)
     28     JsonDictionary = json.load(Jpath)

~\AppData\Local\conda\conda\envs\tensorkeras2\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder

~\AppData\Local\conda\conda\envs\tensorkeras2\lib\json\decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

~\AppData\Local\conda\conda\envs\tensorkeras2\lib\json\decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我得到的只是 JSONDecodeError: Expecting value: line 1 column 1 (char 0)。 我已经看到针对相同错误代码提出的其他解决方案,但它们似乎都不是解决方案。是代码问题还是输入问题?

【问题讨论】:

  • 代码看起来不错,也许你有一个空的json 文件?
  • @Cartucho 我已经重新检查了所有文件,但它们似乎都不是空的。我也检查了格式,一切似乎都是 json 有效
  • 我认为问题在于json.load(Jpath),因为Jpath 是str,但json.load 需要一个类似文件的对象-docs.python.org/2/glossary.html#term-file-like-object。顺便问一下,为什么你同时拥有json.loads(file_s) 和json.load(Jpath)?
  • @Cartucho 即使我完全删除该行,我也会遇到同样的问题。仍然在另一行, file_data = json.loads(file_s) 行。老实说,我的目标是将所有 json 文件数据加载到字典中,以便之后按类别分析它们。即使我从参数中删除字符串,它也会给我相同的结果
  • 我认为你应该写完整的错误输出,这样会更容易帮助你。

标签: python json file load


【解决方案1】:

检查您的文件是否有BOM。
右键单击文件>属性>大小。 (例如:859 字节) 用 Notepad++ 打开它并更改编码(顶部栏菜单)。
如果选择“UTF-8-BOM”,则它是带有 BOM 的文件。否则问题是另一个问题。 将其更改为“UTF-8”并保存。
再次检查大小,应该少了 3 个字节。 (例如:856 字节)。

现在,如果这解决了问题,您可以只使用原始文件,只需添加编码“utf-8-sig”(而不是默认的“utf-8”)。它将处理 BOM(如果存在,则无需检查)。

你可以使用:

open("", "r", encoding="utf-8-sig") # utf-8-sig takes care of BOM

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-30
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多