如何解决美丽的汤-XML解析-属性错误异常处理空值
我有以下代码:
from bs4 import BeautifulSoup
import pandas as pd
import sqlite3
conn = sqlite3.connect('test.db')
inputFile = open("test.xml","r")
data = BeautifulSoup(inputFile,'xml')
stop_points = data.find_all('Location')
l = []
df = pd.DataFrame(columns=['Id','One','Two','Three','Four','Five'])
pos= 0
for stop in stop_points:
l.append(stop.get('id'))
l.append(stop.find('One').text)
l.append(stop.find('Two').text)
l.append(stop.find('Three').text)
l.append(stop.find('Four').text)
l.append(stop.find('Five').text)
df.loc[pos] = l
l = []
pos+=1
print(df)
df.to_sql('Location',conn,if_exists='replace',index=False)
但是,在某些情况下,“位置”并不具有所有元素(“一个”,“两个”,“三个”,“四个”或“五个”)。当脚本迭代到没有所有这些元素的位置时,将引发“属性错误异常”:
Exception has occurred: AttributeError
'NoneType' object has no attribute 'text'
因此,我认为在尝试将'.text'追加到列表之前,需要进行状态检查。但是,我真的不想添加五个“ IF”语句,因为这不是很优雅。
作为参考,下面是XML文件的示例:
<Location id="00000">
<Translation>
<One>111111</One>
<Two>222222</Two>
<Four>-5.0</Four>
<Five>50.0</Five>
</Translation>
</Location>
<Location id="11111">
<Translation>
<One>111111</One>
<Two>222222</Two>
<Three>-5.0</Three>
<Five>50.0</Five>
</Translation>
</Location>
<Location id="22222">
<Translation>
<One>111111</One>
<Two>222222</Two>
</Translation>
</Location>
解决方法
假设您的html看起来像这样:
locations = """
<locations>
<Location id="00000">
<Translation>
<Easting>111111</Easting>
<Northing>222222</Northing>
<Longitude>-5.0</Longitude>
<Latitude>50.0</Latitude>
</Translation>
</Location>
<Location id="00001">
<Translation>
<Easting>4444</Easting>
<Northing>5555</Northing>
<Longitude>-6.0</Longitude>
<Latitude>70.0</Latitude>
</Translation>
</Location>
</locations>
"""
类似这样的东西:
stop_points = data.select('location')
columns=['Id','One','Two','Three','Four']
rows = []
for s in stop_points:
row = []
row.append(s['id'])
row.extend(s.text.strip().split('\n'))
rows.append(row)
pd.DataFrame(rows,columns=columns)
应输出:
Id One Two Three Four
0 00000 111111 222222 -5.0 50.0
1 00001 4444 5555 -6.0 70.0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。