0

I want to store a value from an XML file as a variable in Python to use it for calculations later on. The value shall be the speed of sound. The XML file contains different types of liquid mediums, temperature and speed of sounds. I am loading the XML file with etree.

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'):

The XML looks like this:

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

I come up to this point, but then I don't know how to store it as a variable.

Thanks.

!!!SOLVED!!!

I did it this way:

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

The value is stored and I can also use it for other calculations.

Tiger1982
  • 19
  • 4
  • It is unclear what the problem is. Please post a [mcve] with code and XML as text (not as an image). – mzjn Nov 17 '22 at 14:45
  • I corrected my post. – Tiger1982 Nov 17 '22 at 14:51
  • Even though that is not well-formed XML, it appears that you have an element with a `SpeedofSound=" 1480"` attribute and that you want to store the 1480 value in a variable. – mzjn Nov 17 '22 at 14:59
  • Exactly, I want to store it as SoS and use it for further calculations. My calculations are working when I put in the value manually in the code for SoS – Tiger1982 Nov 17 '22 at 15:10
  • Anybody who can help here? – Tiger1982 Nov 21 '22 at 11:55
  • See https://stackoverflow.com/q/14323335/407651 – mzjn Nov 21 '22 at 12:08
  • I finish now with print(SoS.attribute), but I always get the error that SoS is not defined – Tiger1982 Nov 21 '22 at 12:14
  • The XML is an output from a different software and I cannot change it. However, I found now a solution: df = pd.read_xml(folder_path2+filename8) SoS = df["SpeedofSound"].dropna().to_numpy() – Tiger1982 Nov 23 '22 at 09:09

0 Answers0