B.jour
B.jour

Reputation: 543

Python : editing several files : loop on a list of file

Hy everyone,

I have thousands of three column .csv Ascii files with this format:

"1;6774.64;210.00"

I would like to transform them into do a list I can edit into python like this :

[6774.64, 210.00]

I've starded to try to do some import like :

p = open('ruby-Ne008.csv')
linelist = [line for line in p.readlines()]

but then how to remove the " character , and replace the ; character as a column separator.

thanks!

Upvotes: 1

Views: 63

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

Something like this:

>>> import csv
>>> from itertools import chain
>>> with open('ruby-Ne008.csv') as f:
    reader = csv.reader(f, delimiter = ';' )
    lis = list(chain.from_iterable(map(float,row[1:]) for row in reader))
    print lis
...     
[6774.64, 210.0]

Upvotes: 2

lagdotcom
lagdotcom

Reputation: 64

linelist = [[float(x) for x in line.split(';')[1:]] for line in p.readlines()]

Is that good enough?

Upvotes: 1

Related Questions