3 Cs
3 Cs
f = open("e:\\files\\file1.txt")
v=0
c=0
u=0
l=0
o=0
data = f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=' ' and ch!='\n':
o+=1
print("Total Vowels in file :",v)
print("Total Consonants in file n :",c)
print("Total Capital letters in file :",u)
print("Total Small letters in file :",l)
print("Total Other than letters :",o)
f.close()
OUTPUT:
Total Vowels in file : 16
Total Consonants in file n : 30
Total Capital letters in file : 2
Total Small letters in file : 44
Total Other than letters : 4
Write a program to read and display file content line by line
Program–21 :
with each word separated by #.
OUTPUT:
India#is#my#country#
I#love#python#
Python#learning#is#fun#
Write a program to read the content of file line by line and
Program–22 : write it to another file except for the lines that contains ‘a'
letter in it.
f1 = open("e:\\files\\file2.txt")
f2 = open("e:\\files\\file2copy.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print(“## File Copied Successfully! ##”)
f1.close()
f2.close()
OUTPUT:
## File Copied Successfully! ##
NOTE: After copy content of file2copy.txt
one two three four
five six seven
eight nine ten
bye!
Write a program to display the word with maximum length
Program–23 :
from a text file.
f=open("g:\\files\\story.txt","r")
lword=''
for t in f.readlines():
for r in t.split():
if len(r)>len(lword):
lword=r
print("maximum length word is ",lword)
OUTPUT:
maximum length word is Aeroplane
Write a menu-program to do the following:
1. To count occurrences of words ‘to’ and ‘the’
Program–24 : 2. To append the contents of one file to another.
3. To read a file line by line and display every line along with
line no. in sentence case.
def count():
f=open('e:\\files\\poem.txt','r')
tocnt=0
thecnt=0
data=f.read()
listwords=data.split()
for word in listwords:
if word.lower()=="to":
tocnt+=1
elif word.lower=="the":
thecnt+=1
print("No. of words 'to' and 'the' in file",tocnt+thecnt)
f.close()
#Alternative way
def count():
f=open('e:\\files\\poem.txt','r')
count=0
lines=f.readlines()
for line in lines:
words=line.split()
print(words)
tocnt=words.count("to")
thecnt=words.count("the")
count=count+tocnt+thecnt
print("No. of words 'to' and 'the' in file",count)
f.close()
#function to append the contents of one file to another
def append():
infile=input("Enter the filename to read from:")
outfile=input("Enter the filename to append into:")
outf=open('e:\\files\\'+outfile,'a')
with open('e:\\files\\'+infile,'r') as f:
content=f.read()
outf.write('\n')
outf.write(content)
f.close()
outf.close()
print("File processed successfully")
#function to display every line of file in sentence case along with line no.
def sentencecase():
f = open("e:\\files\\poem.txt","r")
lcnt=0
for line in f:
line = line[0].upper()+line[1:].lower()
#line=line.capitalize()
lcnt = lcnt+1
print(lcnt,line)
f.close()
choice=0
while choice!=4:
print("\t\t1. To count words 'to' and 'the'")
print("\t\t2. To append")
print("\t\t3. To display lines in sentence case")
print("\t\t4. To exit")
choice=int(input("Enter your Choice [ 1 - 4 ]: "))
if choice==1:
count()
elif choice==2:
append()
elif choice==3:
sentencecase()
elif choice==4:
break
NOTE:
If Content of poem.txt is
deep in to the river
here come the way
If Content of newpoem.txt is
This is my poem.
OUTPUT:
import pickle
numerals={1000:'M',900:'CM',500:'D',400:'CD',100:'C',90:'XC',50:'L',40:'XL',
10:'X',9:'IX',5:'V',4:'IV',1:'I'}
f=open("e:\\files\\decroman.bin","wb")
pickle.dump(numerals,f)
f.close()
f=open("e:\\files\\decroman.bin","rb")
num=pickle.load(f)
print("Dictionary Decimal-Roman Convertor:",num)
f.close()
dec_num=int(input("Enter a decimal number:"))
roman=""
val=list(num.keys())
sym=list(num.values())
temp=dec_num
while temp!=0:
i=0
while(temp<val[i]):
i+=1
quot=temp//val[i]
roman=roman+sym[i]*quot
temp=temp-quot*val[i]
print("Corresponding Roman No:",roman)
OUTPUT:
Dictionary Decimal-Roman Convertor: {1000: 'M', 900: 'CM', 500: 'D', 400:
'CD', 100: 'C', 90: 'XC', 50: 'L', 40: 'XL', 10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'}
Enter a decimal number:45
Corresponding Roman No: XLV
Write a program to create a binary file emp.dat storing
employee records in the form of empno, name, salary,
allowances, deductions. The program should input the name of
Program–26 :
employee and search it in binary file emp.dat and display the
netsalary of employee.
[Note: netsalary=salary+allowances-deductions]
with open("e:\\files\\emp.dat","rb") as f:
while True:
emp=pickle.load(f)
except EOFError:
pass
found=False
for rec in emp:
if rec[1]==ename:
netsalary=rec[2]+rec[3]-rec[4]
print("Employee Name:",rec[1],"-> NetSalary:",netsalary)
found=True
break
if found==False:
print("Employee record doesnot exists!!!")
OUTPUT:
import pickle
import os
while True:
#while choice!=7:
print("\t\t1. To create binary file")
print("\t\t2. To append record")
print("\t\t3. To display all records")
print("\t\t4. To search record")
print("\t\t5. To update record")
print("\t\t6. To delete record")
print("\t\t7. To exit")
choice=int(input("Enter your Choice [ 1 - 7 ]: "))
if choice==1:
create()
elif choice==2:
append()
elif choice==3:
display()
elif choice==4:
search()
elif choice==5:
update()
elif choice==6:
delete()
elif choice==7:
break
OUTPUT:
1. To create binary file
2. To append record
3. To display all records
4. To search record
5. To update record
6. To delete record
7. To exit
Enter your Choice [ 1 - 7 ]: 1
Enter Roll Number :1
Enter Name :lavina
Enter Marks :23
Add More ?(y/n)y
Enter Roll Number :2
Enter Name :rohit
Enter Marks :56
Add More ?(y/n)n
1. To create binary file
2. To append record
3. To display all records
4. To search record
5. To update record
6. To delete record
7. To exit
Enter your Choice [ 1 - 7 ]: 2
Enter Roll Number :3
Enter Name :shobha
Enter Marks :54
1. To create binary file
2. To append record
3. To display all records
4. To search record
5. To update record
6. To delete record
7. To exit
Enter your Choice [ 1 - 7 ]: 3
## All Student Records ##
RollNo Name Marks
--------------------------------------------------------
1 lavina 23
2 rohit 56
3 shobha 54
1. To create binary file
2. To append record
3. To display all records
4. To search record
5. To update record
6. To delete record
7. To exit
Enter your Choice [ 1 - 7 ]: 4
Enter Roll number to search :5
####Sorry! Roll number not found ####
1. To create binary file
2. To append record
3. To display all records
4. To search record
5. To update record
6. To delete record
7. To exit
Enter your Choice [ 1 - 7 ]: 5
Enter Roll number to update :2
## Name is : rohit ##
## Current Marks is : 56 ##
Enter new marks :100
## Record Updated ##
1. To create binary file
2. To append record
3. To display all records
4. To search record
5. To update record
6. To delete record
7. To exit
Enter your Choice [ 1 - 7 ]: 4
Enter Roll number to search :2
## Name is : rohit ##
## Marks is : 100 ##
1. To create binary file
2. To append record
3. To display all records
4. To search record
5. To update record
6. To delete record
7. To exit
Enter your Choice [ 1 - 7 ]: 6
Enter Roll number to delete :3
## Record Deleted ##
1. To create binary file
2. To append record
3. To display all records
4. To search record
5. To update record
6. To delete record
7. To exit
Enter your Choice [ 1 - 7 ]: 7
Write a program to create a CSV file delimited by ‘#’ and store
employee records in the form of empno, name, salary. The
Program–28 : program should input empno and search and display the name
& salary of corresponding employee. Also display appropriate
message, if record is not found.
import csv
with open('e:\\files\\employee.csv',mode='w') as csvfile:
mywriter = csv.writer(csvfile,delimiter='#')
ans='y'
emprec=[]
while ans.lower()=='y':
eno=int(input("Enter Employee Number "))
name=input("Enter Employee Name ")
salary=int(input("Enter Employee Salary :"))
emprec.append([eno,name,salary])
ans=input("Add More ?")
print("## Data Saved... ##")
mywriter.writerows(emprec)
OUTPUT:
import csv
OUTPUT:
import csv
import os
index=0
for row in urec:
if row[0]==uname:
found=True
break
index=index+1
f.close()
if found==False:
print("User record not found!!!")
else:
urec.pop(index)
f=open("e:\\files\\users.csv","w",newline='')
csvwriter = csv.writer(f)
csvwriter.writerows(urec)
print("User record deleted!!!")
f.close()
else:
print("File not found!!!")
if ch==1:
for row in csvreader:
if csvreader.line_num==1:
continue
print(row)
elif ch==2:
for row in csvreader:
if csvreader.line_num==1:
continue
print(','.join(row))
else:
for row in csvreader:
if csvreader.line_num==1:
print(row[0].upper(),'\t\t',row[1].upper(),'\t',row[2].upper())
continue
print(row[0],'\t\t',row[1],'\t\t',row[2])
f.close()
else:
print("File not found...Create the file")
choice=0
student=[]
while choice!=9:
print("\t1. To create csv file")
print("\t2. To search")
print("\t3. To update")
print("\t4. To delete")
print("\t5. To append")
print("\t6. To count user records")
print("\t7. To display records")
print("\t9. To exit")
choice=int(input("Enter your Choice [ 1 - 9 ]: "))
if choice==1:
create()
elif choice==2:
search()
elif choice==3:
update()
elif choice==4:
delete()
elif choice==5:
append()
elif choice==6:
count()
elif choice==7:
display()
elif choice==9:
break
OUTPUT:
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 1
Enter UserID: ekta
Enter password: 1234
Enter usertype->'a'-admin,'t'-teacher,'s'-student):t
Add More ?n
##Records File Created... ##
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 6
Total number of users: 1
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 5
Enter UserID: amit
Enter password: ans123
Enter usertype->'a'-admin,'t'-teacher,'s'-student):t
##User Record Appended... ##
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 7
1. As List
2. As Comma Separated Values
3. In Tabular Form
Enter your choice:1
['ekta', '1234', 't']
['amit', 'ans123', 't']
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 2
Enter username to search:ekta
Password: 1234 UserType: t
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 3
Enter userid whose password is to be updated:ekta
Enter new password:123456
User record updated!!!
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 6
Total number of users: 2
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 7
1. As List
2. As Comma Separated Values
3. In Tabular Form
Enter your choice:3
USERID PASSWORD USERTYPE
ekta 123456 t
amit ans123 t
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 4
Enter userid to delete record: ekta
User record deleted!!!
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 7
1. As List
2. As Comma Separated Values
3. In Tabular Form
Enter your choice:3
amit,ans123,t
1. To create csv file
2. To search
3. To update
4. To delete
5. To append
6. To count user records
7. To display records
9. To exit
Enter your Choice [ 1 - 9 ]: 9