我目前正在使用lxml接收一个XML文件,然后从根元素创建一个数据帧。我基本上在使用this example。我这样做是为了能够对数据进行一些数学/建模
我希望实现的下一步是能够将数据写回xml文档。在脚本的其他地方,我使用了root.insert
,因为我可以强制在特定位置插入索引,以保持xml文档整洁一致
是否有一种方法可以为数据帧中的每一行使用类似root.insert(position, data)
的东西写出数据帧的每一行,其中dataframes列标题是标记
示例XML
<Root_Data>
<SomeData></SomeData>
<SomeOtherData></SomeOtherData>
<Weather>
<WxId>1</WxId>
<Temp>20></WxId>
<WindSpeed>15</WindSpeed>
</Weather>
# We will insert more weather here - I can find this position index. Assume it is 3.
<SomeMoreData></SomeMoreData>
<Root_Data>
数据帧:
ID Temp Windspeed
2 25 30
3 30 15
4 15 25
我会提供一些迄今为止我已经尝试过的代码——但实际上,我已经空手而归地找到了如何将数据帧中的行插入到xml文档中,而不需要自己手动将xml构造为字符串(不是很好——标题可能会改变,这就是为什么我希望使用列标题作为标记的原因)
预期结果
<Root_Data>
<SomeData></SomeData>
<SomeOtherData></SomeOtherData>
<Weather>
<WxId>1</WxId>
<Temp>20></WxId>
<WindSpeed>15</WindSpeed>
</Weather>
<Weather>
<WxId>2</WxId>
<Temp>25></WxId>
<WindSpeed>30</WindSpeed>
</Weather>
<Weather>
<WxId>3</WxId>
<Temp>30></WxId>
<WindSpeed>15</WindSpeed>
</Weather>
<Weather>
<WxId>4</WxId>
<Temp>15></WxId>
<WindSpeed>25</WindSpeed>
</Weather>
<SomeMoreData></SomeMoreData>
<Root_Data>
到目前为止的示例代码:
from lxml import etree
import pandas as pd
tree = etree.parse('example.xml')
root = tree.getroot()
#Load into dataframe
for node in root:
res=[]
df_cols = ["WxId","Temp", "WindSpeed"]
res.append(node.attrib.get(df_cols[0]))
for el in df_cols[1:]:
if node is not None and node.find(el) is not None:
res.append(node.find(el).text)
else:
res.append(None)
rows.append({df_cols[i]: res[i]
for i, _ in enumerate(df_cols)})
out_df = pd.DataFrame(rows, columns = df_cols)
out_df = out_df[~out_df['Temp'].isnull()] #Proxy for good / bad data. Remove nulls.
#Now, write from data frame back to root so we can structure the XML before writing to file.
# ? Unknown method
另一种方法,以防您的列未定义或将来可能增加
您可以使用
to_xml
将数据帧转换为xml:现在,您可以使用
lxml
在第一个子Weather
和最后一个子Weather
之前插入数据,或者在原始xml文件中的某个位置插入扩展数据FYI,您可以使用
pd.read_xml
将xml转换为数据帧相关问题 更多 >
编程相关推荐