CS Proiject
CS Proiject
DOCUMENTATION
NAME OF THE SOURCE FILE: 4.22 PI-14
ALGORITHM
Define the function capitalize_Sentence.
Attempt the following steps, catching any file-
related errors (e.g., if the file does not exist):
Open the source file "test report.txt" in
read mode.
Open the target file "file1.txt" in write mode.
Repeat the following steps for each line in the
source file:
Read a line from the source file.
If the line is empty (end of file), exit the loop.
Remove trailing whitespace from the line.
Initialize an empty string capitalized_str to
store the capitalized sentence.
Add the uppercase of the first character in the line
to capitalized_str (if the line has content).
For each character in the line starting from the
second character:
If the character is a period ("."), add it to
capitalized_str.
o Increment the index and check if there’s
capitalized_str.
Otherwise, add the character to
capitalized_str as it is.
Write the capitalized sentence to the target file,
followed by a newline.
Close both the source and target files.
If a FileNotFoundError occurs, print an error
message indicating the source file does not exist.
Call the function capitalize_Sentence.
CODE
def capitalize_Sentence():
f1= open("test report.txt","r")
f2=open("filel.txt", "w")
while 1:
line fl.readline()
if not line:
break
line= line.rstrip()
lineLength=len (line)
str =’’
str=str + line[0].upper()
i=1
while i<lineLength:
if line[i]==".":
str=str + line[i]
i=i+1
if i >= lineLength:
break
str=str + line [i].upper()
else:
str=str + line [i]
i=i+1
f2.write (str)
else:
print("Source file does not exist")
f1.close()
f2.close()
capitalize_Sentence ()
OUTPUT
Python is an object oriented language.
It is a "dynamically typed" language.
You do not need to declare variables before using them.or declare their type.
every variable in Python is an object.
python supports two types of numbers-integers and floating point numbers.
Python is simple in syntax and understanding.
AIM
Write a program to insert /append a record in the
binary file “student.dat”
DOCUMENTATION
(integer).
Store the entered data as a list: [roll_no, name,
marks].
Append this list to record.
Ask the user if they wish to enter more records.
If the user enters 'N' (no), exit the loop. Otherwise,
continue.
3.Open a file named "student" in write-binary ("wb")
mode.
4. Save the record list to the file using pickle.dump().
5. Print a message indicating that the record has been
added.
6.Close the file.
CODE
import pickle
record= []
while true:
roll_no=int (input("Enter student Roll no :"))
name=input ("Enter the student Name:")
marks=int (input ("Enter the marks obtained :"))
data=[roll_no, name, marks]
record.append (data)
choice=input("Wish to enter more records (Y/N)?: ")
if choice.upper() -- 'N':
break
f=open("student", "wb")
pickle.dump(record, f)
print("Record Added")
f.close()
OUTPUT
Enter student Roll no :2
Enter the student Name: Radhika
Enter the marks obtained : 490
Wish to enter more records (Y/N)?: y
Enter student Roll no :3
Enter the student Name: Shaurya
Enter the marks obtained : 450
Wish to enter more records (Y/N)?: n
Record Added
AIM
Write a program to count the number of records
present in “student.csv” file
Student.csv file contains ;-
['Name', 'Class', 'Marks']
['Rinku', 'XII', ' 90 deg ]
['Veenu', ^ r X * I' , ' 88' ]
['Ajay', ' X * I' , ' 78 deg * 1
['Sushant', x' , ' 70 deg * 1
['Radhika', ' T * X' , '80']
['Shaurya', overline ^ prime X' , ' 90 deg
['Deepak', X * I' , ' 60 deg ]
['Jatin', 'XII', '70']
['Himesh', ^ prime XI * I' , '78']
['Vinay', ' X * I' , '99']
>>>
DOCUMENTATION
ALGORITHM
Import the csv module to handle CSV file
operations.
Open the file "student.csv" in read mode
('r'), and assign it to the variable f.
Create a CSV reader object csv_reader using
csv.reader(f) to read the contents of the file.
Initialize a counter variable c to 0. This will keep
track of the number of records.
For each row in csv_reader:
Increment the counter c by 1.
Print the total number of records using the value of
c.
Close the file f.
CODE
import csv
f=open(“student.csv”,’r’)
csv_reader=csv_reader(f)
c=0
for row in csv_reader:
c=c+1
print(“No. of records are:”,c)
f.close()
OUTPUT
No of records are: 11