从文本文件中读取json的问题

时间:2014-07-11 17:10:45

标签: python json python-3.x

我在txt文件中有一个json字符串,我之后尝试读取它以执行其他一些操作。它看起来像这样:

with open('code test.txt', 'r', encoding=('UTF-8')) as f:
    x = json.load(f)

我知道json是有效的,但我得到了:

Traceback (most recent call last):
  File "C:\Python33\lib\json\decoder.py", line 368, in raw_decode
    obj, end = self.scan_once(s, idx)
StopIteration

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 334, in <module>
        user_input()
      File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 328, in user_input
        child_remover()
      File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 280, in child_remover
        x = json.load(f)
      File "C:\Python33\lib\json\__init__.py", line 274, in load
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
      File "C:\Python33\lib\json\__init__.py", line 319, in loads
        return _default_decoder.decode(s)
      File "C:\Python33\lib\json\decoder.py", line 352, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "C:\Python33\lib\json\decoder.py", line 370, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded

我使用此website来检查字符串是否有效。如果我使用.loads(),我会收到不同的错误:

Traceback (most recent call last):
  File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 334, in <module>
    user_input()
  File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 328, in user_input
    child_remover()
  File "C:\Users\rodrigof\Desktop\xml test\xml extraction.py", line 280, in child_remover
    x = json.loads(f)
  File "C:\Python33\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Python33\lib\json\decoder.py", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

最初json被嵌入我的脚本中:

json_text="""json stuff here"""

并没有出现任何错误。关于如何解决这个问题的任何想法???

运行python 3.3.3以防万一。

谢谢!

修改

在txt上只是一些随机(有效)的json,我得到了同样的问题。这是我试过的其中一个:

{"data":
    {"mobileHelp":
        {"value":
            {
            "ID1":{"children": [1,2,3,4,5]},
            "ID2":{"children": []},
            "ID3":{"children": [6,7,8,9,10]}
            }
        }
    }
}

哪个和jsonlint.com一样有效。

3 个答案:

答案 0 :(得分:6)

您的文件一开始就包含UTF-8 BOM character。 UTF-8 不需要BOM ,但特别是Microsoft工具仍坚持添加一个。

使用utf-8-sig编码打开文件:

>>> open('/tmp/json.test', 'wb').write(b'\xef\xbb\xbf{"data":\r\n    {"mobileHelp":\r\n        {"value":\r\n            {\r\n            "ID1":{"children": [1,2,3,4,5]},\r\n            "ID2":{"children": []},\r\n            "ID3":{"children": [6,7,8,9,10]}\r\n            }\r\n        }\r\n    }\r\n}')
230
>>> import json
>>> with open('/tmp/json.test', encoding='utf8') as f:
...     data = json.load(f)
... 
Traceback (most recent call last):
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 367, in raw_decode
    obj, end = self.scan_once(s, idx)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/__init__.py", line 271, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/__init__.py", line 316, in loads
    return _default_decoder.decode(s)
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 351, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.3/json/decoder.py", line 369, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
>>> with open('/tmp/json.test', encoding='utf-8-sig') as f:
...     data = json.load(f)
... 
>>> data
{'data': {'mobileHelp': {'value': {'ID2': {'children': []}, 'ID3': {'children': [6, 7, 8, 9, 10]}, 'ID1': {'children': [1, 2, 3, 4, 5]}}}}}

请注意,从Python 3.4开始,您会在此处收到更有用的错误消息:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/json/__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python3.4/json/__init__.py", line 314, in loads
    raise ValueError("Unexpected UTF-8 BOM (decode using utf-8-sig)")
ValueError: Unexpected UTF-8 BOM (decode using utf-8-sig)

答案 1 :(得分:2)

不确定第二个错误的代码是什么样的,但看起来你传递json.loads文件对象而不是字符串。尝试:

with open('code test.txt', 'r', encoding=('UTF-8')) as f:
    x = json.loads(f.read())

或没有换行符:

with open('code test.txt', 'r', encoding=('UTF-8')) as f:
    x = json.loads(f.read().replace('\n', ''))

答案 2 :(得分:1)

作为另一种选择,这将更容易解决这个问题。

json.loads(open('test.txt').read().decode('utf-8-sig'))