2

I try to read a csv file which I need to convert into a list of floats for evaluation. The list looks like this:

[['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'],...]

but actually it has more than 17000 additional entries. The list I need should look like this:

[['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], ...]

So far I managed to get a single line (last element in list) into this format but not all the list. Here is what I've got so far:

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)

Out:

[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]  

I would appreciate any help and also some tips when it comes to list comprehension. Thanks!

5 Answers 5

1

You need to put the last 2 lines inside the for loop, and also check your indentation:

for j in range(1, len(data)):
    for i in data[j]:
        dt = i.split(';')
        da = [float(v) for v in dt]
        print(da)
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply try:

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

output:

[['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 Comments

Thanks! But unfortunately I get an IndexError: list index out of range. Do you have an idea why?
almost perfect! now it creates a list in a list in a list like that [[[a, b, c]], [[a, b. c]],...]
0

try the following

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

and you will get:

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

Comments

0

Apparently, your csv file uses a ;, and not a ,.

I think you should try specifying the delimiter of your csv file. I haven't tested it, but you could do it like this:

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)

Comments

0

Working with what you have. csv.reader can be passed a delimiter argument.

import csv

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

reader supports one-time direct iteration of an open file handle within the context like so:

for data in reader:
    print(data)

If you need to use data in reader outside the context scope, bind a list containing its data to some other name inside the context scope.

import csv

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.