繁体   English   中英

读取包含多个JSON对象的文件(python)

[英]Reading file that contains multiple JSON objects (python)

我已经从Google搜索API结果中创建了一个JSON文件。 我正在尝试读取文件并解析对象。

每个搜索结果都是一个JSON数组,如下所示。 我在一个JSON文件中有200个这样的数组。

{
  "kind": "customsearch#result",
  "title": "text here",
  "htmlTitle": "text here",
  "link": "link here",
  "displayLink": "text here",
  "snippet": "text here",
  "htmlSnippet": "text here",
  "cacheId": "ID string",
  "formattedUrl": "text here",
  "htmlFormattedUrl": "link here",
  "pagemap": {
  "metatags": [
    {
      "viewport": "width=device-width, initial-scale=1"
    }
  ],
  "Breadcrumb": [
    {
      "title": "text here",
      "url": "link here",
    },
    {
      "title": "text here",
      "url": "link here",
    },
    {
      "title": "text here",
      "url": "link here",
    },
    {
      "title": "text here",
      "url": "link here",
    }
  ]
}

我在将JSON文件读入json.load(s)时遇到问题。

如何阅读此文件并开始解析项目?

def ingest_json(input):
try:
    with open(input, 'r', encoding='UTF-8') as f:
        json_data = json.loads(f)
except Exception:
    print(traceback.format_exc())
    sys.exit(1)

抛出此错误:

TypeError: the JSON object must be str, 
bytes or bytearray, not 'TextIOWrapper'

def ingest_json(input):
try:
    with open(input, 'r', encoding='UTF-8') as f:
        json_data = json.load(f)
except Exception:
    print(traceback.format_exc())
    sys.exit(1)

抛出此错误:

 raise JSONDecodeError("Extra data", s, end)
                   json.decoder.JSONDecodeError: Extra data: line 269 
                   column 2 (char 10330)

json.loads() ,“ s”代表字符串,因此仅适用于字符串类型。

json.load()绝对是您想要的方法,尽管它对json的格式非常明确,并且根据规范,单个JSON文件只能包含单个JSON对象。

尝试将数据拆分为多个文件,每个文件都包含一个对象,或者在解析之前将python中的对象按字符串拆分。 另外,请检查json.loads是否可以忽略结尾的逗号? 用于处理尾随逗号问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM