In [1]:
import csv
dt=open("data2.csv")
for i in dt:
print(i)
print(dt)
In [2]:
# USING csv.reader() function
csv.reader() arguments: reader(iterable [, dialect='excel'] , [optional keyword args])
description:
- iterable: CSV file name is passed
- dialect: format of the file, default is excel
- optional arguments: here we can pass delimiter argument which by default is ',' but can be '\t', etc also
In [3]:
import csv
with open('profData.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
NOTE: by using the with keyword, we dont need to close the file
As we can see that the rows of the csv file is returned as a list, now we can access these list and perform operation on them
reading a file with tab as delimiter(i.e. data separator)
In [4]:
import csv
with open('profData.csv', 'r',) as file:
reader = csv.reader(file, delimiter = '\t') # note we have passed a \t as delimiter
for there in reader:
print(row)
In [11]:
# using csv.writer() function
Function details : csv.writer()
writer(fileobj [, dialect='excel'] ,[optional keyword args])
- fileobj: the file handle
- dialect: format of the file, default is excel
- optional arguments: here we can pass delimiter argument which by default is ',' but can be '\t', etc also
In [15]:
import csv
with open('protagonist.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["SN", "Movie", "Protagonist"])
writer.writerow([1, "Lord of the Rings", "Frodo Baggins"])
writer.writerow([2, "Harry Potter", "Harry Potter"])
with open('protagonist.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
In [16]:
# using the csv.writerows() function
In [17]:
import csv
csv_rowlist = [["SN", "Movie", "Protagonist"], [1, "pirates of the caribbean", " Captain Jack Sparrow"],
[2, "john wick", "john wick"]]
with open('protagonist2.csv', 'w') as file:
writer = csv.writer(file)
writer.writerows(csv_rowlist)
with open('protagonist2.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)