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)
[['27465307'], ['27459855'], ['27451353']...]
for r in reader:
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 a
for 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.