我在一个.xml文件中有以下10行:
<EmpInfo Location="Pune" Name="John">
<EmpInfo>
<EmpInfo Location="Pune" Name="Sam">
<EmpInfo>
<EmpInfo Location="Pune" Name="George">
<EmpInfo>
<EmpInfo Location="Mumbai" Name="Sera">
<EmpInfo>
<EmpInfo Location="Delhi" Name="Jon">
<EmpInfo>
<EmpInfo Location="Mumbai" Name="Josh">
<EmpInfo>
<EmpInfo Location="Pune" Name="Alex">
<EmpInfo>
<EmpInfo Location="Mumbai" Name="Lee">
<EmpInfo>
<EmpInfo Location="Delhi" Name="Ron">
<EmpInfo>
<EmpInfo Location="Mumbai" Name="Sara">
<EmpInfo>
我试过这样做。。但它不起作用:
counter=0
infoDict={}
pointers = header.getElementsByTagName('EmpInfo')
for pointer in pointers:
namelist=[]
pointerobj={}
if counter==0:
name=pointer.getAttribute("Location")
basename=pointer.getAttribute("Name")
namelist.append(name)
basenamelist.append(basename)
else:
basename=pointer.getAttribute("Location")
if pointer.getAttribute("Location") in basenamelist:
name=pointer.getAttribute("Name")
namelist.append(name)
else:
name=pointer.getAttribute("Name")
namelist.append(name)
#basenamelist.append(basename)
print("Location:: ",basename)
print("Name:: ",namelist)
counter=counter+1
infoDict.update({basename:namelist})
我希望结果打印在字典中,如:
infoDict = {
Pune : [John,Sam,George,Alex],
Mumbai : [Sera,Josh,Lee,Sara],
Delhi : [Jon,Ron]
}
我试图在mongoDB中插入这个结果。 在字典中,键必须是位置,值应该是数组。 我的实际应用程序很长,但我想先在其中完成这个小模块
下面是一段代码,使用
re
表示正则表达式,使用pandas
表示数据管理(使用名为my_file.txt
的文件替换为您的文件名):或者,如果您愿意,可以选择两行:
对于某些花式打印(您可以write it into a new file代替打印):
相关问题 更多 >
编程相关推荐