CSV FILE IN PYTHON
In data file handling we have cover the concept of text file and binary file .as we have discussed in our
course there are three types of files
Text file
Binary file
Csv file
Note: the separator character of csv file is called delimeter .Default delimeter is comma(,),tab(“\
t”),colon(:) ,pipe(|) character()
Q1:What is the similarities and difference between text file and csv file
Csv stands for comma separated value
It used for storingdata in tabular format
Each record consist of field separated by commas(delimiter)
Each line of a field is a record
NOTE: DEFINE CSV FILE
A CSV is a comma-separated values file, which allows data to be saved in a
tabular format. CSVs look like a ordinary spreadsheet but with a .csv extension.
To save your spreadsheet as a CSV,
1. Open your file using the spreadsheet application
2. Click File, then choose Save As
3. Choose the location where you want to save your file. Below, you'll see a
Save as type option, choose CSV(Comma delimited), and finally,
click Save button.
4. The application may show a message stating that some of the features in
your workbook might be lost if you save it as CSV. That is to say, any
form of formatting such a bold text will not be saved. Just click Yes to
continue.
ADVANTAGES OF CSV FILE
To better organize large amounts of data
[Link]
1 WAP TO READ AND DISPLAY THE CONTENT OF CSV FILE OUTPUT
file=open(r”C:\Users\lenovo\Desktop\[Link]”,”r”) NAME,PNO
a1=[Link]() TOM,987456123
print(a1) HARRY,321456789
MAC,741258963
SAM,123654789
JOHN,375198426
2 WAP TO DISPLAY THE USE OF IN FUNCTION IN PYTHON OUTPUT
name=input(“enter name:”) enter name:JOHN
file=open(r”C:\Users\lenovo\Desktop\[Link]”,”r”) JOHN,375198426
for line in file:
if name in line:
print(line)
3 WAP TO COUNT TOTAL NO LINE FROM FILE OUTPUT
f=open(“C:\\Users\\lenovo\\Desktop\\[Link]”,”r”) 1
n1=0 2
for line in f: 3
n1 +=1 4
print(n1) 5
6
NOTE: EACH LINE OF A FILE IN CSV IS RECORD
ADVANTAGE OF CSV FILE
Easier to create because it is in human readable format
We can import and export csv file easily
Capable of storing big data
A Python module is a file containing Python definitions and statements. A module can
define functions, classes, and variables. A module can also include runnable code. Grouping
related code into a module makes the code easier to understand and use. It also makes the
code logically organized.
MACHINTOS
UNIX
MS DOS WINDOWS
OTHER OS
If in open function you
have not given new line
argument in csv file then
acc to os it will take its
default value
For ex:
A=open(“[Link]”,’w’)
<WriterObject>.writerows() Writes multiplerows of data on
the writer object
ROLE OF WRITER OBJECT
The Csv .Writer() Function Returns A Writer Object That Convert The User Data Into Delimited
String .This String Can Later Be Used To Write Into Csv File Using The Writerow() And Writerows()
1 import csv
f=open(r"C:\Users\user\Desktop\csv assignment\[Link]","w")
s_writer=[Link](f)
s_writer.writerow(["ROLLNO","NAME","MARKS"])
rec=[]
while True:
r=int(input("ENTER ROLL NO:"))
n=input("ENTER NAME:")
m=int(input("ENTER MARKS:"))
lst=[r,n,m]
[Link](lst)
ch=input("DO YOU WANT TO ENTER MORE RECORDS?(Y/N)")
if ch=="n":
break
for i in rec:
s_writer.writerow(i)
[Link]()
2 import csv
f=open(r"C:\Users\user\Desktop\csv assignment\
[Link]","w",newline='')
s_writer=[Link](f)
s_writer.writerow(["ROLLNO","NAME","MARKS"])
rec=[]
while True:
r=int(input("ENTER ROLL NO:"))
n=input("ENTER NAME:")
m=int(input("ENTER MARKS:"))
lst=[r,n,m]
[Link](lst)
ch=input("DO YOU WANT TO ENTER MORE RECORDS?(Y/N)")
if ch=="n":
break
for i in rec:
s_writer.writerow(i)
[Link]()
3 import csv
f=open(r"C:\Users\user\Desktop\csv assignment\[Link]","w")
s_writer=[Link](f,delimiter=';')
s_writer.writerow(["ROLLNO","NAME","MARKS"])
rec=[]
while True:
r=int(input("ENTER ROLL NO:"))
n=input("ENTER NAME:")
m=int(input("ENTER MARKS:"))
lst=[r,n,m]
[Link](lst)
ch=input("DO YOU WANT TO ENTER MORE RECORDS?(Y/N)")
if ch=="n":
break
for i in rec:
s_writer.writerow(i)
[Link]()