-3

I have got a car.csv, which has lines like this:

vhigh,vhigh,2,2,small,low,unacc

I want to get:

[vhigh,vhigh,2,2,small,low,unacc].

But with this code

import csv
a = []
with open("car.csv", 'r') as f:
    reader = csv.reader(f)
    for line in f:
        a.append([line]);

I get

['vhigh,vhigh,2,2,small,med,unacc\n'].

Can someone help me?

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
kassio
  • 57
  • 1
  • 12
  • I wasn't aware that [vhigh,vhigh,2,2,small,low,unacc] is the same as ['vhigh','vhigh','2','2','small','low','unacc'] – kassio Nov 19 '16 at 14:37

2 Answers2

4

When you create the reader, iterate over that instead of over the file so do:

for line in reader:
    a.append(line)

or if you just want all the lines as a list

a = list(reader)

putting that all together:

with open("car.csv", "r") as f:
    reader = csv.reader(f)
    a = list(reader)
Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
-1

Split the content of the line

line.split(',')
hpelleti
  • 342
  • 1
  • 6