Pixelle
Pixelle

Reputation: 15

Problem treating .csv file datas in Python

I am working with Python 2.7 under Ubuntu 18.04 and I am treating some datas from a .csv file. In order to pass them in my script, I need to have them in a list in a specific format. Here is what it should look like:

data = [('15', '10', '11', '17'),
        ('18', '18', '17', '18'),
        ('12', '17', '17', '18'),
        ('14', '12', '17', '14'),
        ('15', '11', '19', '17')]

each "value" contained in the .csv file is a serie of numbers surrounded by () like that "('15', '10', '11', '17')".

my .csv file looks like that:

('15', '10', '11', '17');
('18', '18', '17', '18');
('12', '17', '17', '18');
('14', '12', '17', '14');
('15', '11', '19', '17')

and the script reading the csv file is this one :

import csv

data = []

with open('logsTESTII.csv', 'r') as f:
    reader = csv.reader(f, delimiter = ';')
    for row in reader:
        data.append(list(reader))

print (data)

no matter what I do, I tried many variation of this script or .csv file structure, I always get weird results like that:

[[["('18', '18', '17', '18')", ''], ["('12', '17', '17', '18')", ''], ["('14', '12', '17', '14')", ''], ["('15', '11', '19', '17')"], [], []]]

I just need a list with all my datas , one after the other separated by a comma.

How could I proceed ? Please, this is driving me insane.

Thanks in advance, Pixelle

Upvotes: 0

Views: 79

Answers (3)

Chris Fowl
Chris Fowl

Reputation: 566

Seems to me that you misunderstood the .csv file index. A row in your file looks like this:

('15', '10', '11', '17');

But I think a row should look like this, what could explain what python did weird things:

15, 10, 11, 17

Sincerly, Chris Fowl.

Upvotes: 1

Lelouch Lamperouge
Lelouch Lamperouge

Reputation: 8421

  1. The delimiter parameter refers to the delimiter for each field, not each line. So the delimiter here is , not ;.
  2. Your file is not a well-formatted csv, so if I knew the structure of the file and had to create a csv, this is what I would do:

with open('logsTESTII.csv') as f, open('out.csv', 'w') as of: for line in f: line = line.replace(";", "").replace(")", "").replace("(", "") of.write(line)

Upvotes: 0

timgeb
timgeb

Reputation: 78800

Your file consists of tuple-literals, it's not really well-formatted csv data.
ast.literal_eval will serve you better than the csv module here.

Demo

$ cat logsTESTII.csv 
('15', '10', '11', '17')
('18', '18', '17', '18')
('12', '17', '17', '18')
('14', '12', '17', '14')
('15', '11', '19', '17')
$ python2.7
>>> from ast import literal_eval
>>> with open('logsTESTII.csv') as f:
...     data = [literal_eval(line) for line in f]
... 
>>> data
[('15', '10', '11', '17'),
 ('18', '18', '17', '18'),
 ('12', '17', '17', '18'),
 ('14', '12', '17', '14'),
 ('15', '11', '19', '17')]

Upvotes: 2

Related Questions