Read A CSV File And Store It In A List
In this topic you are going to read a CSV file and the contents of the file will be stored as a list.
The syntax for storing in the List is
list = [ ] # Start as the empty list
list.append(element) # Use append( ) to add elements
For example all the row values of “sample.csv” file is
stored in a list using the following program
import csv
# other way of declaring the filename
inFile= ‘c:\ \pyprg\\sample.csv’
F=open(inFile, ’r’)
reader= csv.reader(F)
# declaring array
arrayValue = [ ]
# displaying the content of the list
for row in reader:
array Value, appendfrow)
print(row)
F.close( )