0% found this document useful (0 votes)
45 views15 pages

Yash Pratical File

class 12 cs 20 project question

Uploaded by

Yash Rao
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)
45 views15 pages

Yash Pratical File

class 12 cs 20 project question

Uploaded by

Yash Rao
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/ 15

Computer Science

Practical File
Name:
YASH

Class:
XII – F

Roll No:

35
Index
S. No Questions T. Sign
. Write a program to read all data from
01 "data.txt" and write in another file
"data.txt" except the vowels

Write a program to count number of vowels


02 in a file ("data.txt").

Write a program to count number of spaces


03 from the file ("data.txt").
Write a program to count all the upper case
04 characters from the file ("data.txt").

Write a program in python to display all the


05 lines from the text file("data.txt") with first
character in uppercase.
Q6. Write a program in python to read first
06 line from the text file("data.txt")

Write a program in python to read entire


07 content of text file ("data.txt")
A binary file "STUDENT.DAT" has structure
08 [admission_number, Name, Percentage].
Write a function countrec() in Python that
would read contents of the file
"STUDENT.DAT" and display the details of
those students whose percentage is above
75. Also display number of students scoring
above 75%.
Write a function addrec() in Python to add
09 more new records at the bottom of a binary
file "STUDENT.dat", assuming the binary file
is containing the following structure:
[Roll Number, Student Name]

(i.) Write a user defined function


10 CreateFile() to input data for a record and
add to "Book.dat". (ii). Write a function
CountRec(Author) in Python which accepts
the Author name as parameter and count
and return number of books by the given
Author are stored in the binary file
"Book.dat"

Write a function SCOUNT() to read the


11 content of binary file "NAMES.DAT" and
display number of records (each name
occupies 20 bytes in file) where name
begins from "S" in it.
Write a function SCOUNT() to read the
12 content of binary file "NAMES.DAT" and
display number of records (each name
occupies 20 bytes in file) where name
begins from "S" in it
Write code to delete a row from csv file.
13
Write a program to add/insert records in
14 file "data.csv". Structure of a record is roll
number, name and class.
Write a program to copy the data from
15 "data.csv" to "temp.csv"

Write a program to calculate the sum of all


16 the marks given in the file "marks.csv.
Records in "marks.csv" are as follows:
Rolino, Name, Marks 1, Suman, 67 2,
Aman,71 3, Mini, 68 4, Amit, 80

Write a program to count number of


17 records present in "data.csv" file.

If you have already created a header row


18 then you have to use append mode to insert
data. Observe the following code:
The given program is used to connect with
19 MySQL and show the name of the all the
record from the table “stmaster” from the
database “oraclenk”. You are required to
complete the statements so that the code
can be executed properly
Q20. Aarvi is trying to connect Python with
20 MySQL for her project. Help her to write the
python statement on the following:–
(i) Name the library, which should be
imported to connect MySQL with Python.
(ii) Name the function, used to run SQL
query in Python.
(iii) Write Python statement of connect
function having the arguments values as :
Host name :192.168.11.111
User : root
Password: Admin
Database : MYPROJECT

Q1. Write a program to read all data from "data.txt" and write in another file
"data.txt" except the vowels.

Ans.
f = open("data.txt", 'r')

f1= open("dest.txt", 'w')

cd = f.read()

vow = "aeiouAEIOU"

for i in cd:

if (not i in vow):

f1.write(i)

f1.close()

f.close()
Q2. Write a program to count number of vowels in a file ("data.txt").

Ans.

f = open("data.txt", 'r')

I = f.read()

c=0

vow = "aeiouAEIOU"

for i in 1:

if i in vow:

c = c +1

print("Total vowels are = ", c)

Q3. Write a program to count number of spaces from the file ("data.txt").

Ans.

f=open("data.txt",'r')

1=f.read()

c=0

for i in 1:

if (i.isspace() and i!= '\n'): c=c+1

print("Total spaces are = ",c)

Q4. Write a program to count all the upper case characters from the file
("data.txt").

Ans.

f=open("data.txt", 'r')

1=f.read()

c=0

for i in 1:
if (i.isupper()):

c=c+1

print("Total uppercase characters”)

Q5. Write a program in python to display all the lines from the text
file("data.txt") with first character in uppercase.

Ans.

f=open("data.txt",'r')

1=f.readlines()

for i in 1:

print((i[0].upper()+i[1:-1]))

Q6. Write a program in python to read first line from the text file("data.txt")

Ans.

f = open("data.txt", 'r')

d = f.readline()

print(d)

Q7. Write a program in python to read entire content of text file ("data.txt")

Ans.

f = open("data.txt", 'r')

d = f.read()

print(d)

Q8. A binary file "STUDENT.DAT" has structure [admission_number, Name,


Percentage]. Write a function countrec() in Python that would read contents
of the file "STUDENT.DAT" and display the details of those students whose
percentage is above 75. Also display number of students scoring above 75%.
Ans.

import pickle

def countrec():

fobj=open("student.dat","rb")

num = 0

try:

while True:

rec=pickle.load(fobj)

if rec[2]>75:

num = num + 1

print(rec[0],rec[1],rec[2])

except:

fobj.close()

return num()

Q9. Write a function addrec() in Python to add more new records at the
bottom of a binary file "STUDENT.dat", assuming the binary file is
containing the following structure:

[Roll Number, Student Name]

Ans.

import pickle

def addrec():

fobj=open("student.dat", "ab")

rollno=int(input("Roll Number: "))

sname=input("Student Name:")

rec=[rollno,sname]
pickle.dump(rec, fobj)

fobj.close()

addrec()

Q10.(i.) Write a user defined function CreateFile() to input data for a record
and add to "Book.dat". (ii). Write a function CountRec(Author) in Python
which accepts the Author name as parameter and count and return number
of books by the given Author are stored in the binary file "Book.dat"

Ans.(i)

import pickle

def createFile():

f=open("Book.dat","ab")

BookNo=int(input("Book Number: "))

Book_name=input("Name:")

Price = int(input("Price: "))

Author = input("Author:")

rec=[BookNo, Book_Name, Author, Price]

pickle.dump(rec,f)

f.close()

Ans.(ii)

def CountRec(Author):

f=open("Book.dat","rb")

num = 0

try:

while True:

rec=pickle.load(f)

if Author==rec[2]:

num = num + 1
except:

f.close()

return num

Q11.Write a function SCOUNT() to read the content of binary file


"NAMES.DAT" and display number of records (each name occupies 20 bytes
in file) where name begins from "S" in it.

Ans.

def SCOUNT():

S=''

count=0

f=open('Names.dat', 'rb'):

while True:

s = f.read(20)

if len(s)!=0:

if s[0].lower()=='s':

count+=1

print('names beginning from "S" are', count)

Q12.Write a function SCOUNT() to read the content of binary file


"NAMES.DAT" and display number of records (each name occupies 20 bytes
in file) where name begins from "S" in it

Ans.

def SCOUNT():

S=''

count=0

f=open('Names.dat', 'rb'):

while True:

s = f.read(20)
if len(s)!=0:

if s[0].lower()=='s':

count+=1

print('names beginning from "S" are', count)

Q13. Write code to delete a row from csv file.

Ans.

import csv

record = list()

custname= input("Please enter a customer name to delete:")

with open('cust.csv', 'r') as f:

data = csv.reader(f)

for row in data:

record.append(row)

for field in row:

if field == custname:

record.remove(row)

with open('cust.csv', 'w') as f:

writer = csv.writer(f)

writer.writerows (record)

Q13. Write code to insert multiple rows in the above csv file.

from csv import writer with open("cust.csv","a", newline="\n")

as f:

Ans.

dt = writer(f)

while True:
sno= int(input("Enter Serial No:"))

cust_name = input("Enter customer name:")

city = input("Enter city:")

amt = int(input("Enter amount:"))

dt.writerow([sno, cust_name, city, amt])

print("Record has been added.")

print("Want to add more record? Type YES!!!")

ch = input()

ch = ch.upper()

if ch=="YES":

print("******* ***”)

else:

break

Q14. Write a program to add/insert records in file "data.csv". Structure of a


record is roll number, name and class.

Ans.

import csv

field = ["Rollno", "Name", "Class"]

f = open("data.csv", 'w')

d=csv.writer(f)

d.writerow(field)

ch='y'

while ch=='y' or ch=='Y':

rn=int(input("Enter Roll number: "))

nm = input("Enter name: ")

cls = input("Enter Class: ")


rec= [rn,nm,cls]

d.writerow(rec)

ch=input("Enter more record??(Y/N)")

f.close()

Q15. Write a program to copy the data from "data.csv" to "temp.csv"

Ans.

import csv

f=open("data.csv","r")

f1=open("temp.csv",'w')

d=csv.reader(f)

d1=csv.writer(f1)

for i in d:

d1.writerow(i)

f.close()

f1.close()

Q16. Write a program to calculate the sum of all the marks given in the file
"marks.csv. Records in "marks.csv" are as follows:

Rolino, Name, Marks 1, Suman, 67 2, Aman,71 3, Mini, 68 4, Amit, 80

Ans.

import csv

f=open("marks.csv","r")

d=csv.reader(f)

next(f)

s=0

for i in d:

s=s + int(i[2])

print("Total Marks are ",s)


f.close()

Q17. Write a program to count number of records present in "data.csv" file.

Ans.

import csv

f = open("data.csv", "r")

d = csv.reader(f)

next(f)

r=0

for row in d:

r = r+1

print("Number of records are", r)

Q18.If you have already created a header row then you have to use append
mode to insert data. Observe the following code:

Ans.from csv import writer

def f CSVwrite():

f = open("ICCtop5.csv","a")

r = int(input("Enter rank:"))

b= input("Enter batsman name:")

t = input("Enter team of the player:")

rt = int(input("Enter rating:"))

dt writer(f)

dt.writerow([r,b,t,rt])

print("Record has been added.")

f.close()

f_CSVwrite()
Q.19The given program is used to connect with MySQL and show the name
of the all the record from the table “stmaster” from the database “oraclenk”.
You are required to complete the statements so that the code can be
executed properly.

Ans.

import mysql.connectoraspymysql

dbcon=pymysql.connect(host=”localhost”, user=”root”, password=”sia@1928”)

if dbcon.isconnected()==False:

print(“Error in establishing connection:”)

else:

cur=dbcon.cursor()

query=”select * from stmaster”

cur.execute(query)

resultset = cur.fetchmany(3)

for row in resultset:

print(row)

dbcon.close( )

Q20. Aarvi is trying to connect Python with MySQL for her project. Help her
to write the python statement on the following:–

(i) Name the library, which should be imported to connect MySQL with
Python.

(ii) Name the function, used to run SQL query in Python.

(iii) Write Python statement of connect function having the arguments


values as :

Host name :192.168.11.111

User : root

Password: Admin

Database : MYPROJECT
Ans.

(i) import mysql.connector

(ii) execute ( sql_query )

(iii) mysql.connector.connect(host = ”192.168.11.111”, user = ”root”, password =


”Admin”, database = ”MYPROJECT”)

You might also like