Python 我有一个JSON dict列表,我想检查其中一个JSON结构中的名称是否包含字符子集
正如标题所述,我想检查name字段中的某一组字符。如果name字段包含字符,我希望将其作为字符串写入新变量Python 我有一个JSON dict列表,我想检查其中一个JSON结构中的名称是否包含字符子集,python,json,Python,Json,正如标题所述,我想检查name字段中的某一组字符。如果name字段包含字符,我希望将其作为字符串写入新变量 Master_Reports包含JSON结构列表。我在下面附上了一个结构的一部分 [{'directory': {'item': [{'last-modified': '2020-03-13 16:17:38', 'name': '0001628280-20-003532-index-headers.html', 'type': 'text.gif', 'si
Master_Reports
包含JSON结构列表。我在下面附上了一个结构的一部分
[{'directory': {'item': [{'last-modified': '2020-03-13 16:17:38',
'name': '0001628280-20-003532-index-headers.html',
'type': 'text.gif',
'size': ''},
{'last-modified': '2020-03-13 16:17:38',
'name': '0001628280-20-003532-index.html',
'type': 'text.gif',
'size': ''},
{'last-modified': '2020-03-13 16:17:38',
'name': 'wkhs-20191231_htm.xml',
'type': 'text.gif',
'size': '1315390'},
{'last-modified': '2020-03-13 16:17:38',
'name': 'wkhs-20191231_lab.xml',
'type': 'text.gif',
'size': '705817'},
{'last-modified': '2020-03-13 16:17:38',
'name': 'wkhs-20191231_pre.xml',
'type': 'text.gif',
'size': '469764'},
{'last-modified': '2020-03-13 16:17:38',
'name': 'wkhs-2019x10kxex1041xi.htm',
'type': 'text.gif',
'size': '117747'},
{'last-modified': '2020-03-13 16:17:38',
'name': 'wkhs-2019x10kxexx412de.htm',
'type': 'text.gif',
'size': '22977'},
{'last-modified': '2020-03-13 16:17:38',
'name': 'wkhs-2019x10xkxex1042s.htm',
'type': 'text.gif',
'size': '287964'},
{'last-modified': '2020-03-13 16:17:38',
'name': 'wkhs-2019x10xkxex211.htm',
'type': 'text.gif',
'size': '2275'},
{'last-modified': '2020-03-13 16:17:38',
'name': 'wkhs-2019x10xkxex231.htm',
'type': 'text.gif',
'size': '2523'}],
'name': '/Archives/edgar/data/1425287/000162828020003532',
'parent-dir': '/Archives/edgar/data/1425287'}}
以下是我访问JSON结构的代码:
for report in master_reports:
current_report = master_reports
for i in range(0,len(current_report['directory']['item'])):
if 'def' in current_report['directory']['item'][i]['name']:
file_def = current_report['directory']['item'][i]['name']
这给了我一个错误:
TypeError:列表索引必须是整数或切片,而不是str
master\u reports
是单个项目的列表:请注意左前括号。因此,当前报告[]
无效。相反,试试看
current_report = master_reports[0]
由于
def
不在任何名称中,因此您现在完全没有输出。您正在使用for循环以单个对象作为报告来迭代master_报告
获取current\u report=master\u reports
也将当前报告作为列表提供
因此,尝试获取当前_报告['directory']
会出错,因为您试图使用str值获取索引
请尝试以下内容:
for report in master_reports:
for item in report['directory']['item']:
if 'def' in item['name']:
file_def = item['name']