为什么我在以前工作的代码上出现 TypeError?
Why am I getting TypeError on code that worked previously?
我有这段代码可以遍历 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
如果这是一个真正的 TypeError,我不明白为什么我的代码之前能够工作。为什么这只是代码中的问题,我该如何解决?
编辑:粘贴了 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"
}
},
一个漂亮的 pythonic 方法是使用异常:
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}")
您的代码将 运行 通过并提示您失败的原因
如果将 for line in data:
替换为 for line in data['contains']
,您的代码可能会正常工作
可能 JSON 架构之前没有 "contains"
级别。
原来这是一个非常简单的修复。所有 JSON 文件都在一个容器中(查看我在问题中发布的部分,它是第二行,“包含”:)。在那之后,我能够成功地删除那个容器及其 open/closing 括号和代码 运行 。感谢大家的帮助。
我有这段代码可以遍历 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
如果这是一个真正的 TypeError,我不明白为什么我的代码之前能够工作。为什么这只是代码中的问题,我该如何解决?
编辑:粘贴了 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"
}
},
一个漂亮的 pythonic 方法是使用异常:
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}")
您的代码将 运行 通过并提示您失败的原因
如果将 for line in data:
替换为 for line in data['contains']
可能 JSON 架构之前没有 "contains"
级别。
原来这是一个非常简单的修复。所有 JSON 文件都在一个容器中(查看我在问题中发布的部分,它是第二行,“包含”:)。在那之后,我能够成功地删除那个容器及其 open/closing 括号和代码 运行 。感谢大家的帮助。