代码之家  ›  专栏  ›  技术社区  ›  sgmagnotta

为什么以前工作过的代码会出现TypeError?

  •  1
  • sgmagnotta  · 技术社区  · 2 年前

    我有这个代码来迭代json文件。用户指定要提取的层,然后这些层的名称保存在inputLabels中,这个for循环从这些层提取数据:

    with open(inputfilename, 'r', encoding='utf8', newline='\r\n') as f:
            data = json.load(f)
            for line in data:
                if line['label'] in inputLabels:
                    elements = [(e['body']['value']).replace(" ", "_") + "\t" for e in line['first']['items']]
                    outputData.append(elements)
    

    我在一年前写了这段代码,从那以后已经多次运行,没有出现任何问题,但今天运行时我收到了一个TypeError。

        if line['label'] in inputLabels:
    TypeError: string indices must be integers
    

    如果这是一个真正的类型错误,我不明白为什么我的代码以前能够工作。为什么这只是代码中的一个问题,我该如何解决它?

    编辑:粘贴json的一部分:

    {
      "contains": [
        {
          "total": 118,
          "generated": "ELAN Multimedia Annotator 6.2",
          "id": "xxx",
          "label": "BAR001_TEXT",
          "type": "AnnotationCollection",
          "@context": "http://www.w3.org/ns/ldp.jsonld",
          "first": {
            "startIndex": "0",
            "id": "xxx",
            "type": "AnnotationPage",
            "items": [
              {
                "id": "xxx",
                "type": "Annotation",
                "body": {
                  "purpose": "transcribing",
                  "format": "text/plain",
                  "language": "",
                  "type": "TextualBody",
                  "value": ""
                },
            "@context": "http://www.w3.org/ns/anno.jsonld",
            "target": {
              "format": "audio/x-wav",
              "id": "xxx",
              "type": "Audio"
            }
          },
          {
            "id": "xxx",
            "type": "Annotation",
            "body": {
              "purpose": "transcribing",
              "format": "text/plain",
              "language": "",
              "type": "TextualBody",
              "value": "Dobar vam"
            },
            "@context": "http://www.w3.org/ns/anno.jsonld",
            "target": {
              "format": "audio/x-wav",
              "id": "xxx",
              "type": "Audio"
            }
          },
          {
            "id": "xxx",
            "type": "Annotation",
            "body": {
              "purpose": "transcribing",
              "format": "text/plain",
              "language": "",
              "type": "TextualBody",
              "value": "Je"
            },
            "@context": "http://www.w3.org/ns/anno.jsonld",
            "target": {
              "format": "audio/x-wav",
              "id": "xxx",
              "type": "Audio"
            }
          },
    
    3 回复  |  直到 2 年前
        1
  •  0
  •   Walter Tross    2 年前

    如果替换了 for line in data: 具有 for line in data['contains']

    也许JSON模式没有 "contains" 之前的水平。

        2
  •  0
  •   gnight    2 年前

    一种非常类似Python的方法是使用异常:

    with open(inputfilename, 'r', encoding='utf8', newline='\r\n') as f:
            data = json.load(f)
            for line in data:
                try:
                    if line['label'] in inputLabels:
                        elements = [(e['body']['value']).replace(" ", "_") + "\t" for e in line['first']['items']]
                        outputData.append(elements)
                except Exception as e:
                    print( f"{type(e)} : {e} when trying to use {line}")
    

    您的代码将贯穿整个过程,并给出失败原因的提示

        3
  •  0
  •   sgmagnotta    2 年前

    事实证明,这是一个非常简单的解决方案。所有的JSON文件都在一个容器中(看看我在问题中发布的部分,它是第二行,“包含”:。我能够移除那个容器和它的打开/关闭括号,代码在那之后成功运行。谢谢大家的帮助。