Then in CSV it will be stored like below (Values containing comma will be enclosed in double quotes)
Python Provides CSV module to work with csv file:
Main Functions are:
- reader()
- writer()
- DictReader()
- DictWriter()
- reader() function : This function help us to read the csv file. This function takes a file object and returns a _csv.reader object that can be used to iterate over the contents of a CSV file.
How to read entire data from data.csv file?
Let we try to read the following file ie “data.csv”
data.csv
CODING OUTPUTimport csv
f = open(“data.csv”, ‘r’)
row = csv.reader(f)
for i in row :
print(i) [‘Name’ , ‘Class’ , ‘Subject’]
[‘Amit’ , ‘XII’ , ‘ CS’]
[‘Sumit’ , ‘X’ , ‘IP’]
Notice that each line in the CSV file is returned as a list of strings Python CSV File
Line-wise explanation of the code
- The first line is importing a csv module.
- This line is opening a file (data.csv) in reading mode and f is file object or file handle.
- This line is returning a csv.reader object that can be used to iterate over csv file contents
- We are using loop on the object which is returned by the previous line
f = open(“data.csv”, ‘r’)
row = csv.reader(f)
print(row)
Returns the memory address where csv.reader object is stored Python CSV File
How to read specific column from data.csv?
f = open(“data.csv”, ‘r’)
row = csv.reader(f)
for i in row :
print(i[0] , i[2])
The above code is reading first and
third column from “data.csv” file Name Subject
Amit CS
Sumit IP[‘Name’ , ‘Class’ , ‘Subject’]
[‘Amit’ , ‘XII’ , ‘ CS’]
[‘Sumit’ , ‘X’ , ‘IP’] Python CSV File
If you want to skip the First Row (header of the table)
CODING OUTPUT Actual Data of File import csvf = open(“data.csv”, ‘r’)
row = csv.reader(f)
next(row)
for i in row :
print(i[0] , i[2])
next () function will jump to the next row.
import csv
f = open(“data.csv”, ‘r’)
row = csv.reader(f)
next(row)
next(row)
for i in row :
print(i[0] , i[2])
If we use the next() function two times
then it will skip two lines from beginning [ ‘Amit’ , ‘CS’]
[‘Sumit’ , ‘IP’ ]
[‘Sumit’ , ‘IP’ ]
[‘Name’ , ‘Class’ , ‘Subject’]
[‘Amit’ , ‘XII’ , ‘ CS’]
[‘Sumit’ , ‘X’ , ‘IP’]
[‘Name’ , ‘Class’ , ‘Subject’]
[‘Amit’ , ‘XII’ , ‘CS’]
[‘Sumit’ , ‘X’ , ‘IP’] Python CSV File
_______________________________________________________
If there is any other delimiter other than comma like in the following file
f = open(“data.csv”, ‘r’)
row = csv.reader(f, delimiter = ‘l’)
for i in row :
print(i) [‘id’ , ‘name’ , ’email’ , ‘salary’]
[‘1’ , ‘Amit’ , ‘amit@mail.com’ , ‘25000’]
[‘2’ , ‘Sumit’ , ‘sumit@mail.com’ , ‘30000’]
[‘3’ , ‘Ashu’ , ‘ashu@mail.com’ , ‘40000’]
- writer() function : This function help us to write data in a csv file. It accepts the same argument as the reader() function but returns a writer object (i.e _csv.writer)
There are two main methods used in writing in csv file
- writerow()
- writerows()
Example using writerow()
CODING DATA.CSV import csvf = open(“data.csv”, ‘w’)
wr = csv.writer(f)
wr.writerow([‘Name’ , ‘Class’])
wr.writerow([‘Amit’ , ‘XII’])
f.close( )Name , Class
‘Amit’ , ‘XII’Python CSV File
Example using writerows()
CODING DATA.CSV import csvfields = [‘Name’ , ‘Class’ , ‘Subject’]
rows = [[“Amit” , ‘XII’ , ‘CS’] ,
[‘Sumit’ , ‘X’ , ‘IP’] ,
[‘Ashu’ , ‘XI’ , ‘CS’]]
f = open(“data.csv” , ‘w’ , newline = ‘ ‘)
wr = csv.writer(f)
wr.writerow(fields)
wr.writerows(rows)
f.close
Name, Class, Subject
Amit, XII, CS
Sumit, X, IP
Ashu, XI, CS
Python CSV File
Reading a CSV file with DictReader:
This function(DictReader) is working similar to reader(). This function return lines as a dictionary instead of list.
How to read CSV file in Python class 12?
import csv. f = open(“data.csv”, 'r') row = csv.reader(f).
next(row) for i in row : print(i[0] , i[2]).
next () function will jump to the next row..
import csv. f = open(“data.csv”, 'r') row = csv.reader(f).
next(row) next(row) for i in row : ... .
If we use the next() function two times..
then it will skip two lines from beginning..
What is CSV function class 12?
CSV file A CSV ( Comma Separated Value ) file is a text file that stores tabular data in simple text format separated by a separator. The default separator is comma (,) but any other single character can be used as a separator.
How to create CSV file in Python?
How to Create CSV File in Python?.
Open the CSV file in writing (w mode) with the help of open() function..
Create a CSV writer object by calling the writer() function of the csv module..
Write data to CSV file by calling either the writerow() or writerows() method of the CSV writer object..
Finally, close the CSV file..
What is CSV file in Python with example?
A CSV file (Comma Separated Values file) is a type of plain text file that uses specific structuring to arrange tabular data. Because it's a plain text file, it can contain only actual text data—in other words, printable ASCII or Unicode characters. The structure of a CSV file is given away by its name.