加载json文件时出现Python错误

时间:2016-04-27 04:36:20

标签: python json

with open(filepath) as filedata:
    decks = json.load(filedata)

我使用上面的代码尝试加载由Scrapy Spider创建的JSON文件。我最近重新使用刮刀来获取更新的JSON文件,并且当我尝试加载新文件时,格式应该相同但上面的行通过以下错误。 (请注意,这不会附加到任何现有文件)。

    Traceback (most recent call last):
  File "/Users/tonysitu/Documents/workspace/Mtg/ArchetypeAnalysis/analyze/deck_stats.py", line 215, in launcher
    standard = constructFormat()
  File "/Users/tonysitu/Documents/workspace/Mtg/ArchetypeAnalysis/analyze/deck_stats.py", line 98, in constructFormat
    decks = json.load(deckData)
  File "/Users/tonysitu/anaconda3/lib/python3.4/json/__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/Users/tonysitu/anaconda3/lib/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/Users/tonysitu/anaconda3/lib/python3.4/json/decoder.py", line 346, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 120 column 835 - line 306 column 945 (char 99352 - 254693)

我已经使用网络上的各种应用程序验证了JSON文件,但没有一个引发错误。此外,所有词典都由一组[]括号括起来。我没有改变我的刮刀,数据格式似乎与我以前工作的JSON文件相同。我不确定导致错误的原因。我会很感激任何建议,但我想避免使用json.loads()并逐行浏览文件。

[
{"cards": ["Den Protector", "Duskwatch Recruiter", "Hidden Dragonslayer", "Jace, Vryn's Prodigy", "Sylvan Advocate", "Bounding Krasis", "Nissa, Vastwood Seer", "Reflector Mage", "Tireless Tracker", "Archangel Avacyn", "Dromoka's Command", "Collected Company", "Ojutai's Command", "Canopy Vista", "Evolving Wilds", "Forest", "Fortified Village", "Island", "Lumbering Falls", "Plains", "Port Town", "Prairie Stream", "Clip Wings", "Declaration in Stone", "Den Protector", "Dromoka's Command", "Hidden Dragonslayer", "Negate", "Lantern Scout", "Ojutai's Command", "Sigarda, Heron's Grace", "Tragic Arrogance"], "quantities": ["\n1\n", "\n4\n", "\n1\n", "\n3\n", "\n4\n", "\n4\n", "\n1\n", "\n4\n", "\n3\n", "\n2\n", "\n3\n", "\n4\n", "\n1\n", "\n3\n", "\n4\n", "\n3\n", "\n1\n", "\n2\n", "\n4\n", "\n3\n", "\n1\n", "\n4\n", "\n2\n", "\n2\n", "\n1\n", "\n1\n", "\n1\n", "\n3\n", "\n1\n", "\n1\n", "\n1\n", "\n2\n"], "name": ["Bant Company  by Kevin Sauls Deck"]},
{"cards": ["Deathcap Cultivator", "Duskwatch Recruiter", "Jace, Vryn's Prodigy", "Rattlechains", "Sylvan Advocate", "Bounding Krasis", "Eldrazi Skyspawner", "Nissa, Vastwood Seer", "Reflector Mage", "Tireless Tracker", "Sigarda, Heron's Grace", "Declaration in Stone", "Dromoka's Command", "Nissa, Voice of Zendikar", "Collected Company", "Canopy Vista", "Evolving Wilds", "Forest", "Island", "Lumbering Falls", "Plains", "Prairie Stream", "Yavimaya Coast", "Dispel", "Clip Wings", "Declaration in Stone", "Den Protector", "Felidar Cub", "Negate", "Stratus Dancer", "Lantern Scout", "Ojutai's Command", "Archangel Avacyn"], "quantities": ["\n1\n", "\n4\n", "\n1\n", "\n2\n", "\n4\n", "\n4\n", "\n2\n", "\n1\n", "\n4\n", "\n2\n", "\n1\n", "\n1\n", "\n3\n", "\n1\n", "\n4\n", "\n4\n", "\n4\n", "\n3\n", "\n2\n", "\n4\n", "\n3\n", "\n4\n", "\n1\n", "\n2\n", "\n1\n", "\n1\n", "\n2\n", "\n1\n", "\n2\n", "\n1\n", "\n1\n", "\n2\n", "\n2\n"], "name": ["Bant Company  by Jessy Hefner Deck"]}
...
]

1 个答案:

答案 0 :(得分:0)

解决了,错误是由于我的框架命名空间中的错误而不是实际的JSON文件。请参阅上面的评论。