You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python将XML中的声速值存储为变量用于计算

从XML提取声速值并存储为Python变量的解决方案

需求

从包含不同液体介质、温度及对应声速信息的XML文件中提取声速值,存储为Python变量,用于后续计算。

初始尝试代码

使用etree模块加载XML文件的代码如下:

root = ET.parse(folder_path2+filename8)
SoS = root.find('.//SpeedofSound')
tree = ET.parse(folder_path2+filename8)
root = tree.getroot()
for SoS in root.findall('SpeedofSound'):

对应XML结构

<Medium="Water" SpeedofSound=" 1480" Temperature=" 15"/>

问题卡点

完成上述步骤后,无法将提取到的声速值存储为可直接使用的变量。

最终解决方法

采用以下代码实现声速值的提取与存储:

SoS = df["SpeedofSound"].dropna().to_numpy()

通过该方式已成功将声速值存储为数组变量,可直接用于后续计算。

内容的提问来源于stack exchange,提问作者Tiger1982

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 23:01:54