Beautiful Soup - XML 解析 - 属性错误异常(处理空值)
Beautiful Soup - XML Parsing - Attribute Error Exception (Handling Null Values)
我有以下代码:
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)
但是,在某些情况下 'Location' 并不包含所有元素('One'、'Two'、'Three'、'Four' 或 'Five').当脚本迭代到不包含所有这些元素的位置时,将抛出属性错误异常:
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
我有以下代码:
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)
但是,在某些情况下 'Location' 并不包含所有元素('One'、'Two'、'Three'、'Four' 或 'Five').当脚本迭代到不包含所有这些元素的位置时,将抛出属性错误异常:
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