CSV Files in Python
CSV Files in Python
writerow
import csv module
To import csv module we can use any one of the following syntax:
import csv
from csv import writerow
from csv import writerow, reader
from csv import *
In above statements, first example is used to import the complete csv module therefore we can
access anything from the module using csv and .(dot) symbol.
In second syntax it won’t access the complete code, it will just allow to use writerow() function only
in the program. In next part two functions used from the module and last all the classes, functions
and variables can be access using *.
Note: To save the memory and reduce the program size third syntax can be used for project with a
number of required functions only.
Here the output is displayed according to the list. To get it more clear and concise format we have to
use another for loop to traverse the values separately. Check following code:
from csv import reader
def f_CSVread():
f = open("ICCtop5.csv","r")
dt = reader(f)
data = list(dt)
f.close()
for i in data:
for j in i:
print('\t|',j,end=" ")
print()
f_CSVread()
In the above code we have used nested loop to print each value separately from the list and
formatted using ‘\t\‘ hence the output looks like as following: