如何将字符串数据存储到单个数组中?

用户14447985

我正在从 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将printStackTrace存储到字符串中

如何将字符串数组列表存储到Firebase?

如何将数据(字符串数组)插入到H2数据库?

如何将JSON数组存储为字符串

如何将带空格的字符串数组转换为Javascript中的单个数组列表?

如何保持将数据存储到字符串,直到在数组列表中显示符号?

如何将值从NSMutableArray存储到字符串?

如何将单个数据框列中的多个键值对的字符串分解为python中的新数据框?

将argv []存储到两个数组中,并用特殊字符串(C)分隔

如何将单词对从文件存储到字符串?

如何将字符串数组中的字符串存储到C中的变量中

如何从字符串中拆分单个数据..?

如何将日期选择器中的值存储到字符串变量中

如何将数据存储在要进行字符串化的数组中(javascript)

如何将字符串生成的子字符串存储到字符串数组中?

在c中输入字符串以及如何将字符串存储到变量中

如何将stringbuffer存储到字符串数组中?

如何使用getline将完整字符串存储到字符串数组中?

如何将一个字符串数组存储到另一个字符串数组中?

如何将zsh字符串数组存储(和加载)到文件?

如何将标记化的字符串存储到C中的数组中

如何将数字转换为ASCII值或字符并存储在字符串数组中

VB 将 2 个数据行转换为数组中的单个字符串

如何将词干字符串存储到单个数组中?

将字符串数组存储到数据属性中

如何将多个值从javascript中的单个字符串推送到数组?

如何在 C++ 中将字符串输入存储在单个数组中?

如何将函数参数的字符串值一起存储到单个变量中?

如何将字符串值存储到php中的数组中?