【问题标题】:Json Encoder Expecting Value error in PythonPython中的Json编码器期望值错误
【发布时间】:2019-09-05 06:44:39
【问题描述】:

我有一个 350mb 的大 Json 文件,我想从中提取项目。我使用的代码是:

with open("commitsJson3.json","r", encoding="utf-8-sig") as json_file:
    data = json.load(json_file)


for elem in data['items']:
    for e in elem['commit']:
       if 'message' in e:
           print(elem['commit'][e])

我得到的错误是:

json.decoder.JSONDecodeError:预期值:第 1 行第 2180 列(字符 2179)

我去了特定的列和行,但没有发现任何问题。我试图通过一些在线验证器来验证我的 json,但它崩溃了,因为它太大了。我可以给你看一些样本,但它太大了,希望你能理解。


{“total_count”:3,“incomplete_results”:“False”,“items”:c“site_admin”:False},“committer”:{“login”:“acosding”,“id”:1539,“ node_id”:“ASJKDHASAD”,“avatar_url”:“https://gits-5.s.fe.se/avatars/u/1329?”,“gravatar_id”:“”,“url”:“https://gits-5.s.fe.se/api/v3/users/acollden”,“html_url”:“https://gits-5.s.fe.se/acollden”,“followers_url”:“ https://https://gits-5.s.fe.se/api/v3/users/acollden/followers", "following_url": "https://gits-5.s.fe.se/api/v3/users/acollden/following{/other_user}", "gists_url": "https://gits-5.s.fe.se/api/v3/users/acollden/gists{/gist_id}", "starred_url": "https://https://gits-5.s.fe.se/api/v3/users/acollden/starred{/owner}{/repo}" ,“subscriptions_url”:“https://https://gits-5.s.fe.se/api/v3/users/acollden/subscriptions”,“organizations_url”:“https://gits-5.s.fe.se/api/v3/users/acollden/orgs”,“repos_url”:“https://https://gits-5.s.fe.se/api/v3/users/acollden/repos”,“events_url”:“https://https://gits-5.s.fe.se/api/v3/users/acollden/events{/privacy}”,“received_events_url”:“ https://https://gits-5.s.fe.se/api/v3/users/acollden/received_events", "type": "用户"


如果 Json 文件在如此大的文件中存在如何验证它的问题等,任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 如果我带你去 json 提取它只会进入第 1111 列,你能分享完整的 json 文件吗,或者至少是一个超出line 1 column 2180 (char 2179)的提取物@
  • 我可以分享。你想让我通过电子邮件或其他方式发送给你吗?它的 350mb
  • @lee-pai-long 你要我把它寄给你吗?
  • 350mb 不会传递给大多数电子邮件提供商,您可以尝试查看错误指示的列的提取是否小于 50mb 并使用filebin 吗?
  • @lee-pai-long 这里是filebin.ca/4dlrj3G96n2d/Json4.txt

标签: python json api github


【解决方案1】:

据我所知,您提供的示例格式不正确,如果我尝试 只解码第一部分:

my_json = '{"total_count": 3, "incomplete_results": "False", "items": c "site_admin": False}'

我尝试解析它:

import json

json.loads(my_json, encoding='utf-8-sig')

>>> JSONDecodeError: Expecting value: line 1 column 60 (char 59)

指的是c 缺少引号,那么如果我解决这个问题:

my_json = '{"total_count": 3, "incomplete_results": "False", "items": "c" "site_admin": False}'
print(json.loads(my_json, encoding='utf-8-sig'))

>>> JSONDecodeError: Expecting ',' delimiter: line 1 column 64 (char 63)

这是指items 键后缺少的,。修复此问题后:

my_json = '{"total_count": 3, "incomplete_results": "False", "items": "c", "site_admin": False}'
print(json.loads(my_json, encoding='utf-8-sig'))

>>> JSONDecodeError: Expecting value: line 1 column 79 (char 78)

指的是最后一个False。这可以通过使用false"False" 来解决,具体取决于您希望处理的类型。 但是鉴于您的第一个 False 被视为字符串:

my_json = '{"total_count": 3, "incomplete_results": "False", "items": "c", "site_admin": "False"}'
print(json.loads(my_json, encoding='utf-8-sig'))

>>> {'items': 'c', 'total_count': 3, 'site_admin': 'False', 'incomplete_results': 'False'}

终于成功了

【讨论】:

  • 兄弟,我的文件和原来的不一样。用单引号。我是否应该尝试另一种方法使其双引号。但是我做不到smh....
  • 那么我认为你应该打开另一个问题,因为这个问题已经解决了,如果没有更详细地描述原始文件是什么以及如何修改它,就很难理解发生了什么跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多