将字符串(csv)的子列表转换为float(列表理解)

时间:2018-01-31 07:46:48

标签: python string csv split

我尝试读取一个csv文件,我需要将其转换为浮动列表以供评估。该列表如下所示:

[['Time [s];17063_X;17063_Y;17063_Z;17064_X;17064_Y;17064_Z;17065_X;17065_Y;17065_Z;17067_X;17067_Y;17067_Z;17068_X;17068_Y;17068_Z'], ['0;0.01952;0.04337;0.0242;0.01151;0.04152;0.03236;0.00015;-0.01679;0.05328;0.02872;0.01717;0.09341;0.01452;0.01489;0.07444'], ['0.00042;0.02188;0.04351;0.02803;0.0062;0.04108;0.03312;-0.00529;-0.01412;0.05167;0.02173;0.01377;0.04098;0.00807;0.00246;0.04354'],...]

但实际上它有超过17000个附加条目。我需要的清单应该是这样的:

[['Time [s]', '17063_X', '17063_Y', '17063_Z', '17064_X', '17064_Y', '17064_Z', '17065_X', '17065_Y', '17065_Z', '17067_X', '17067_Y', '17067_Z', '17068_X', '17068_Y', '17068_Z'], [0, 0.01952, 0.04337, 0.0242, 0.01151, 0.04152, 0.03236, 0.00015, -0.01679, 0.05328, 0.02872, 0.01717, 0.09341, 0.01452, 0.01489, 0.07444], ...]

到目前为止,我设法将一行(列表中的最后一个元素)转换为此格式,但不是所有列表。这是我到目前为止所得到的:

import csv

with open(filepath, 'r') as f:
  reader = csv.reader(f)
  data = list(reader)

for j in range(1, len(data)):    # this loop does nothing?!
    for i in data[j]:
    dt = i.split(';')

da = [float(i) for i in dt]
print(da)

输出:

[0.005, 0.0207, 0.02925, 0.02095, 0.02332, 0.04211, 0.02223, 0.0075, -0.01961, 0.05093, 0.02604, 0.00711, 0.06644, 0.00689, -0.00092, 0.04737]  

在列表理解方面,我会感谢任何帮助和一些提示。 谢谢!

5 个答案:

答案 0 :(得分:1)

您需要将最后两行放在for循环中,并检查缩进:

for j in range(1, len(data)):
    for i in data[j]:
        dt = i.split(';')
        da = [float(v) for v in dt]
        print(da)

答案 1 :(得分:1)

您可以尝试:

data=[['Time [s];17063_X;17063_Y;17063_Z;17064_X;17064_Y;17064_Z;17065_X;17065_Y;17065_Z;17067_X;17067_Y;17067_Z;17068_X;17068_Y;17068_Z'], ['0;0.01952;0.04337;0.0242;0.01151;0.04152;0.03236;0.00015;-0.01679;0.05328;0.02872;0.01717;0.09341;0.01452;0.01489;0.07444'], ['0.00042;0.02188;0.04351;0.02803;0.0062;0.04108;0.03312;-0.00529;-0.01412;0.05167;0.02173;0.01377;0.04098;0.00807;0.00246;0.04354']]


print(list(map(lambda x:list(map(lambda y:y.split(';'),x)),data)))

输出:

[['Time [s]', '17063_X', '17063_Y', '17063_Z', '17064_X', '17064_Y', '17064_Z', '17065_X', '17065_Y', '17065_Z', '17067_X', '17067_Y', '17067_Z', '17068_X', '17068_Y', '17068_Z'], ['0', '0.01952', '0.04337', '0.0242', '0.01151', '0.04152', '0.03236', '0.00015', '-0.01679', '0.05328', '0.02872', '0.01717', '0.09341', '0.01452', '0.01489', '0.07444'], ['0.00042', '0.02188', '0.04351', '0.02803', '0.0062', '0.04108', '0.03312', '-0.00529', '-0.01412', '0.05167', '0.02173', '0.01377', '0.04098', '0.00807', '0.00246', '0.04354']]

答案 2 :(得分:0)

尝试以下

s=[["a;b;c"],["d;e;f"]]
[x[0].split(';') for x in s]

你会得到:

[['a', 'b', 'c'], ['d', 'e', 'f']]

答案 3 :(得分:0)

显然,您的csv文件使用的是;,而不是,

我认为您应该尝试指定csv文件的分隔符。我没有测试过,但你可以这样做:

import csv
with open(filepath, 'r') as f:
    reader = csv.reader(f, delimiter = ';')
    # omit the first line  
    for row in reader[1:]:
        da = [float(i) for i in dt]
        print (da)

答案 4 :(得分:0)

使用你拥有的东西。 csv.reader可以传递分隔符参数。

import csv

with open(filepath, 'r') as f:
  reader = csv.reader(f, delimiter=':')

reader支持在上下文中一次性直接迭代打开文件句柄,如下所示:

for data in reader:
    print(data)

如果您需要在上下文范围之外的阅读器中使用数据,请将包含其数据的列表绑定到上下文范围内的其他名称。

import csv

entries = []
with open(filepath, 'r') as f:
  reader = csv.reader(f, delimiter=':')
  entries = list(reader)