RichardAdams
RichardAdams

Reputation: 85

Python CSV reading - I think I'm making a list of lists but I just want a list

This regards Python 2.7

I have a csv file, soq1, that looks like this in Notepad:

csv file in Notepad

I used this code to read in the file and display the contents of the list soq1:

import csv
with open('D:\Users\Richard\Python\soq1.csv','rb') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    soq1=list(readCSV)

print(soq1)
print(type(soq1))
print(len(soq1))

with results of: results

I was expecting soq1 to look like: ['2','3','4','5','6']

In other words, I did not expect to have the extra set of square brackets. Did I create a list of lists?

What did I do wrong?

Upvotes: 2

Views: 2356

Answers (6)

trs
trs

Reputation: 1127

Decided to move my comment to your question to an answer, because I feel like all the answers here do not reflect what you want to achieve.

You get a list of lists because each line in a CSV generally has multiple columns.

If that's not the case, why do all the CSV stuff? You can just read your text file into a flat list.

How do I read a file line-by-line into a list?

lines = tuple(open(filename, 'r'))

Upvotes: 2

ggupta
ggupta

Reputation: 707

Improved the code

import csv
with open('D:\Users\Richard\Python\soq1.csv','rb') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    soq1=list(readCSV) #commnt this line
    for line in readCSV:
        print line
        print type(line)
        print len(line)

#print(soq1)
#print(type(soq1))
#print(len(soq1))

Upvotes: 1

Batman
Batman

Reputation: 8927

The csv.reader creates a list for each line in the file. Which makes sense in most cases, because most csv files aren't a single column.

You can always flatten the list with a comprehension:

foo = [item for inner in outer for item in inner]

Upvotes: 1

perigon
perigon

Reputation: 2095

You didn't do anything wrong - the csv module just returns one list per line (which makes sense for CSVs with multiple entries in each line.)

You can flatten your list using

soq2 = [elt for lst in soq1 for elt in lst]

Although for such a simple file, you don't really need to handle it as a CSV at all (it doesn't matter what the file extension is.) You could just do:

with open(my_file) as f:
    soq1 = [line.strip() for line in f]

Upvotes: 4

JoshuaRLi
JoshuaRLi

Reputation: 1735

Yes, you created a list of lists, but this is intentional because by nature, CSV files are meant to have separate entries delimited by new lines, and separate properties in each entry delimited by commas (why they call it comma separated values).

That is not a proper CSV file, by the way. The convention is that the first line (also known as the header line) should denote comma-separated strings describing what each value means in successive lines/entries.

If you would like to read that file and produce ['2','3','4','5','6'], csv.reader is not suited to your specific use case. You will want to read each separate line and append it to a list, or read the entire file in and split it into a list using \n as a delimiter.

Upvotes: 2

DGS
DGS

Reputation: 6025

Each of the numbers is on a separate line, this is causing the csv reader to think they are separate rows rather than columns. You should change the csv file if you can as that is not the correct format of a csv file for one row.

Upvotes: 1

Related Questions