CBSE Class 12: Python Important Programs 4

Program:10.
Python program to create a CSV file named “student.csv”, to store Roll, Name and Per of students.

#writing records in csv file
import csv
f=open('student.csv','w',newline='')
stdwriter=csv.writer(f,delimiter=',')
stdwriter.writerow(['Rollno','Name','Marks']) # write header row
stdrec= [ 
      ['101', 'Nikhil', '99'], 
     ['102', 'Sanchit', '98'], 
     ['103', 'Aditya', '98'], 
     ['104', 'Sagar', '87'], 
     ['105', 'Prateek', '88'], 
       ] 
stdwriter.writerows(stdrec)
f.close()

Program:11.
Write a Python program to get the details (code, description and price) for multiple items from the user and create a csv file ‘product.csv’ by writing all the details. Also read and display the contents of the file.

import csv
f=open("product.csv","w",newline='')
w=csv.writer(f)
n=int(input('enter the limit: '))
for i in range(n):
    c=input('enter code: ')
    d=input('enter description: ')
    p=int(input('enter price: '))
    l=[c,d,p]
    w.writerow(l)
f.close()

f=open("product.csv","r",newline='')
r=csv.reader(f)
for i in r:
    print(i)

Program:12
Read a text file line by line and display each word separated by a #.

f=open("poem.txt","r")
for line in f.readlines():
    for word in line.split():
        print(word,end="#")
f.close()