0% found this document useful (0 votes)
40 views3 pages

CSV Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

CSV Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

# PYTHON PROGRAM TO READ "STUDENT.

CSV" FILE CONTENTS

#PROGRAM 1

import csv
f=open("student.csv",'r')
csv_reader=csv.reader(f)
for row in csv_reader:
print(row)
f.close()

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#PYTHON PROGRAM TO DEMONSTRATE USE OF OPEN ()


#PROGRAM 2

import csv
with open("student.csv",'r') as csv_file:
reader=csv.reader(csv_file)
rows=[] #list to store the file data
for rec in reader: #copy data into the list rows
rows.append(rec)
print(rows)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#python program to count the number of records present in "student.csv" file


#PROGRAM 3
import csv
f=open("student.csv",'r')
csv_reader=csv.reader(f) #csv_reader is the csv reader object
c=0
for row in csv_reader:
c=c+1
print("no of records are" , c)
f.close()

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#python program to print records in the form of comma separated values
#PROGRAM 4
import csv
f=open("student.csv",'r')
csv_reader=csv.reader(f)
for row in csv_reader:
print(','.join(row))
f.close()

# JOIN() IS A STRING METHOD THAT JOINS ALL VALUES OF EACH ROW WITH COMMA SEPARATOR.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#python program to search records for given students name from csv file
#PROGRAM 5
import csv
f=open("student.csv",'r')
csv_reader=csv.reader(f)
name=input("enter the name to be searched ")
for row in csv_reader:
if(row[0]==name):
print(row)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#program to write student data onto a csv file


#PROGRAM 6
import csv
fields=['NAME','CLASS','YEAR','%']
rows=[['REENA','XII','2005','92'],
['MEENU','XII','2006','94'],
['DEPAK','XII','2007','94'],
['SHWETA','XII','2008','95'],
['YAMINI','XII','2009','96'],
['SABA','XII','2010','95'],
['ASRA','XII','2011','93']]
#name of the csv file
filename='marks.csv'

#writing to csv file

with open(filename,'w',newline='') as f:
#by default, new line is '\r\n'
#creating a csv writer object
csv_w=csv.writer(f,delimiter=',')
csv_w.writerow(fields)
for i in rows:
csv_w.writerow(i)
print("file created")

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#PROGRAM 7
#program to write student data onto a csv file

import csv
fields=['NAME','CLASS','YEAR','PERCENTAGE']

#DATA ROWS OF CSV FILE


rows=[
['HASSAN','XII','2007','89'],
['SEEMA','XII','2008','91'],
['SAMI','XII','2009','93'],
['RAHUL','XII','2010','94'],
['NIDHI','XII','2011','92'],
['HEENA','XII','2012','90'],
['NEETU','XII','2013','98'],
['ASNA','XII','2014','97'],
['MARYUM','XII','2015','96']]
#name of csv file
filename="newmarks.csv"

with open(filename,'w',newline='') as f:
csv_w=csv.writer(f,delimiter=',')
#writing the fields once
csv_w.writerow(fields)
#writing the rows all at once
csv_w.writerows(rows)

print(" all rows written in one go")

You might also like