qeia qeia - 1 year ago 89
Python Question

Read csv file into list of lists without string convertion in Python

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


I want to get:


But with this code

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

I get


Can someone help me?

Answer Source

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

for line in reader:

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download