编辑:我可以打印 rev['contributor'] 一段时间,但每次尝试访问 rev['contributor'] 都会返回以下内容
TypeError: string indices must be integers
原始帖子:我正在尝试使用 xml 从 xml 中提取数据以使用代码进行 dict:
import xmltodict, json
with open('Sockpuppet_articles.xml', encoding='utf-8') as xml_file:
dic_xml = xmltodict.parse(xml_file.read(), xml_attribs=False)
print("parsed")
for page in dic_xml['mediawiki']['page']:
for rev in page['revision']:
for user in open("Sockpuppet_names.txt", "r", encoding='utf-8'):
user = user.strip()
if 'username' in rev['contributor'] and rev['contributor']['username'] == user:
dosomething()
我在 if 语句的最后一行收到此错误:
TypeError: string indices must be integers
奇怪的是,它适用于另一个 xml 文件。