0

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

4
  • Post an excerpt from that csv file please. Commented Oct 24, 2018 at 19:21
  • Try inserting a print(row) in the for loop to see what is being appended. This should help clarify where the trouble is. Commented Oct 24, 2018 at 19:25
  • 1
    Must your CSV data be formatted in such a non-standard way - do you have any control over your input data? Commented Oct 24, 2018 at 19:47
  • I think this whole thing is an xyproblem, as I don't think anyone will provide data in this kind of file format in the first place. Commented Nov 19, 2018 at 12:58

3 Answers 3

2

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')]
Sign up to request clarification or add additional context in comments.

1 Comment

thanks you so much! it works just fine ! never heard about that before.
1

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.

Comments

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

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.