我有这样一个xml文件:
<a>
<b>1</b>
<c>2</c>
<d>
<e>3</e>
</d>
</a>
<a>
<c>4</c>
<f value ="something">5</f>
<g value = "other"></g>
</a>
我要一份有标签和文字的单子。 例如:
[{'b':1, 'c':2, 'e':3}, {'c':4, 'f value="something"':5, 'g value = "other"':None}]
它是一个很大的xml文件,而且不是标准的,所以我只知道<a>
存在,我希望所有信息都在这个标记中。你知道吗
我已经试过漂亮的汤4,但我只能检索文本部分。你知道吗
我的代码
def ProcessXml(xmlFile):
infile = open(xmlFile, 'r')
contents = infile.read()
soup = BeautifulSoup(contents,'xml')
units = soup.find_all('a')
unitsList = []
for i in units:
resultType = i.text,i.next_sibling
resultType = resultType[0].splitlines()
for j in resultType:
if j == '':
resultType.remove(j)
unitsList.append((resultType))
return unitsList
我的输出:
[['1','2','3'],['4','5']]
这是我将使用的代码。这是@Stergios编写的代码的改编。(在python 3中工作)
这是一个非常糟糕的代码,但它确实起到了作用:
结果是:
注意:正如MYGz所提到的,
'g value = "other"'}]
部分无效,因此这是我在其上尝试函数的XML文件:相关问题 更多 >
编程相关推荐