Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3将xml数据转换为变量_Python_Xml_Variables_Attributes - Fatal编程技术网

Python3将xml数据转换为变量

Python3将xml数据转换为变量,python,xml,variables,attributes,Python,Xml,Variables,Attributes,我想从我的太阳能逆变器中读取数据,并将其发布到pvoutput.org。逆变器回答以下xml: <Device Name="StecaGrid 4200" Type="Inverter" Serial="********************" BusAddress="1" NetBiosName="***************" IpAddress="***.***.****.***" DateTime="2020-01-18T16:31:31"> <Measuremen

我想从我的太阳能逆变器中读取数据,并将其发布到pvoutput.org。逆变器回答以下xml:

<Device Name="StecaGrid 4200" Type="Inverter" Serial="********************" BusAddress="1" NetBiosName="***************" IpAddress="***.***.****.***" DateTime="2020-01-18T16:31:31">
<Measurements>
<Measurement Value="225.492" Unit="V" Type="AC_Voltage"/>
<Measurement Value="0.442" Unit="A" Type="AC_Current"/>
<Measurement Value="83.860" Unit="W" Type="AC_Power"/>
<Measurement Value="49.983" Unit="Hz" Type="AC_Frequency"/>
<Measurement Value="470.400" Unit="V" Type="DC_Voltage"/>
<Measurement Value="0.182" Unit="A" Type="DC_Current"/>
<Measurement Value="85.840" Unit="W" Type="DC_Power"/>
<Measurement Value="20.000" Unit="°C" Type="Temp"/>
</Measurements>
</Device>
</root> 
如果有阳光,结果如下所示:

C_Voltage 226.632 V
AC_Current 0.259 A
AC_Power 41.920 W
AC_Frequency 49.980 Hz
DC_Voltage 451.896 V
DC_Current 0.100 A
DC_Power 45.270 W
Temp 18.100 °C
但当值为0时,该属性将被忽略

只有当交流电源>0时,我才想上传数据。如何将这些属性存储在变量或数组中以创建帖子url?

更好的解析器(如beautifulsoup4的lxml)可以简化这项工作。假设以固定的时间间隔对逆变器进行轮询,并且您定期在xml文件中获取数据,您可能希望首先使用以下组合命令安装lxml和Beauty soup:

python3-m pip安装lxml bs4 beautifulsoup4

现在,不管您有什么属性和子标记的xml,您都可以使用beautifulsoup4执行以下方法来获取属性的值

from bs4 import BeautifulSoup as soup
import requests
import lxml

def read_ac_power(file_name):
    data=open(file_name, 'r').read()
    variabul=soup(data,'lxml')
    val=variabul.findAll('measurement')[2]
    return val['value']

AC_Power=float(read_ac_power('inverter'))
print(AC_Power)

#compare the variable and send to server , use POST [or your preferred] method
如果不想存储文件,请删除openfile_名称“r”。读取方法并直接插入数据

解释

本质上是float,它显式地将xml文档的字符串转换为float

from bs4 import BeautifulSoup as soup
import requests
import lxml

def read_ac_power(file_name):
    data=open(file_name, 'r').read()
    variabul=soup(data,'lxml')
    val=variabul.findAll('measurement')[2]
    return val['value']

AC_Power=float(read_ac_power('inverter'))
print(AC_Power)

#compare the variable and send to server , use POST [or your preferred] method
val=variabul.findAll('measurement')[2]  #AC_power is stored as 3rd item...

return val['value'] #returns a string & need conversion