Reputation: 3801
I have a csv
file that has one column of records that looks like this:
test1
test2
test3
...
I imported them into a Python list using this:
import csv
results = []
with open('test.csv', newline='') as inputfile:
for row in csv.reader(inputfile):
results.append(row)
print(results)
That worked fine, however, it prints them out like this:
[['test1'],['test2'],['test3']]
How would I adjust my code to have them print out in a single list instead like this:
['test1','test2','test3']
and, is one output more preferred than the other? I realize it is use-case dependent. I am just curious how others work with imported lists via .csv, and what advantage one would have over the other.
Thanks!
Upvotes: 5
Views: 19215
Reputation: 1363
Try the following code
import csv
results = []
with open('test.csv', newline='') as inputfile:
for row in csv.reader(inputfile):
results.append(row[0])
print(results)
Upvotes: 19