Alistair Weir Alistair Weir - 3 months ago 10
Python Question

CSV.Reader importing a list of lists

I am running the following on a csv of UIDs:

with open('C:/uid_sample.csv',newline='') as f:
reader = csv.reader(f,delimiter=' ')
uidlist = list(reader)


but the list returned is actually a list of lists:

[['27465307'], ['27459855'], ['27451353']...]


I'm using this workaround to get individual strings within one list:

for r in reader:
print(' '.join(r))


i.e.

['27465307','27459855','27451353',...]


Am I missing something where I can't do this automatically with the csv.reader or is there an issue with the formatting of my csv perhaps?

Answer

A CSV file is a file where each line, or row, contains columns that are usually delimited by commas. In your case, you told csv.reader() that your columns are delimited by a space. Since there aren't any spaces in any of the lines, each row of the csv.reader object has only one item. The problem here is that you aren't looking for a row with a single column; you are looking for a single item.

Really, you just want a list of the lines in the file. You could use f.readlines(), but that would include the newline character in each line. That actually isn't a problem if all you need to do with each line is convert it to an integer, but you might want to remove those characters. That can be done quite easily with a list comprehension:

newlist = [line.strip() for line in f]

If you are merely iterating through the lines (with afor loop, for example), you probably don't need a list. If you don't mind the newline characters, you can iterate through the file object directly:

for line in f:
    uid = int(line)
    print(uid)

If the newline characters need to go, you could either take them out per line:

for line in f:
    line = line.strip()
    ...

or create a generator object:

uids = (line.strip() for line in f)

Note that reading a file is like reading a book: you can't read it again until you turn back to the first page, so remember to use f.seek(0) if you want to read the file more than once.