我正在从 xml 文件中提取数据:
import numpy as np
from matplotlib import pyplot as plt
from lxml import etree
data = '/Users/hudaiftikhar/Desktop/eis-modelling/ISEAFramework-master/scenarios/electrical/tesla.xml'
SOC = []
My_Res=[]
doc = etree.parse(data.encode())
print(doc,'doc')
soc = doc.xpath('//CustomDefinitions//MyRser[@class="OhmicResistance"]//MeasurementPointsRow[@ desc="StateOfCharge"]/text()')
for val in soc:
SOC.append(val)
SOC_ = [x.replace('\n\t\t\t\t', '') for x in SOC]
myRes_lud = doc.xpath('//CustomDefinitions//MyRser[@class="OhmicResistance"]//LookupData/text()')
for vals in myRes_lud:
My_Res.append(vals)
Res_ = [x.replace('\n\t\t\t\t\t' , '') for x in My_Res]
Res_update= [x.replace(';\n\t\t\t\t' , '') for x in Res_]
print('soc',SOC_)
print('res',Res_update)
我得到的输出是:
soc ['-5, 0, 7.100000e+00, 1.120000e+01, 16, 2.080000e+01, 2.560000e+01, 3.040000e+01, 3.520000e+01, 4.010000e+01, 4.490000e+01, 4.970000e+01, 5.450000e+01, 5.930000e+01, 6.420000e+01, 69, 7.380000e+01, 7.860000e+01, 8.350000e+01, 8.830000e+01, 9.310000e+01, 9.770000e+01, 100, 105']
res ['0.02597518381655694900, 0.02513715386193249600, 0.02394715132636577100, 0.02325996676357371800, 0.02317075771456176400, 0.02277814077034603900, 0.02267913709322775700, 0.02258569292134297900, 0.02235026503875497600, 0.02222478423822949300, 0.02207606555239715500, 0.02198493491067361700, 0.02188144525929673300, 0.02167985791309091600, 0.02145797158835977700, 0.02137484908165417400, 0.02126561803424023600, 0.02124462299304301700, 0.02123310358079429400, 0.02126287857906075300, 0.02094998489960795500, 0.02073326148328196600, 0.02062489977511897100, 0.02038933084432985300']
但这被存储为数组索引 0 上的字符串,我想要一个用于 soc 和 res 的数组,将每个值单独存储为 float ,而不是单个字符串。如果有人可以帮助我,那将是一个很大的帮助。
你需要使用 split()
像这样:
SOC_ = ['-5, 0, 7.100000e+00, 1.120000e+01, 16, 2.080000e+01, 2.560000e+01, 3.040000e+01, 3.520000e+01, 4.010000e+01, 4.490000e+01, 4.970000e+01, 5.450000e+01, 5.930000e+01, 6.420000e+01, 69, 7.380000e+01, 7.860000e+01, 8.350000e+01, 8.830000e+01, 9.310000e+01, 9.770000e+01, 100, 105']
list_of_soc = SOC_[0].split(",") # splits the first position of the array by the delimiter ","
print(list_of_soc)
这打印:
['-5', ' 0', ' 7.100000e+00', ' 1.120000e+01', ' 16', ' 2.080000e+01', ' 2.560000e+01', ' 3.040000e+01', ' 3.520000e+01', ' 4.010000e+01', ' 4.490000e+01', ' 4.970000e+01', ' 5.450000e+01', ' 5.930000e+01', ' 6.420000e+01', ' 69', ' 7.380000e+01', ' 7.860000e+01', ' 8.350000e+01', ' 8.830000e+01', ' 9.310000e+01', ' 9.770000e+01', ' 100', ' 105']
在打印语句中应用于您的代码,您可以这样做
print('soc',SOC_[0].split(";"))
print('res',Res_update[0].split(";"))
对于浮动,您应该可以float()
像这样简单地使用
SOC_ = [float(x.replace('\n\t\t\t\t', '') for x in SOC)]
Res_update= [float(x.replace(';\n\t\t\t\t' , '') for x in Res_)]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句