0% found this document useful (0 votes)
6 views

Data File Handling Abcd

Uploaded by

Yashmit Singh
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)
6 views

Data File Handling Abcd

Uploaded by

Yashmit Singh
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/ 9

PRACTICAL FILE

COMPUTER SCIENCE

Name: Shivjeet Patra


Class: XII Einstein
1. Count the number of characters excluding spaces.
2. Count upper and lower characters separately excluding spaces.
3. Count numbers, symbols and spaces separately.
4. Count number of vowels and consonants separately.

x=input("enter string")

f = open("text.txt","w")
f.write(x)

f.close()

f = open("text.txt","r")

y = f.read()
f.close()

counts=0
count_lo=0
count_up=0
print(y)
print(type(y))
for i in y:
if i == " ":
counts+=1
elif str(i).islower():
count_lo+=1
elif str(i).isupper():
count_up+=1

print("char = ",len(y)-counts) # counts total characters except


spaces
print("char upper = ", count_up)
print("char lower = ", count_lo)
5. Count the number of words.
6. Count the number of ‘is’ word.
7. Count the number of words starting with ‘a’.
8. Count the number of words ending with ‘s’.
9. Count the number of words starting with ‘the’.
10. Count the number of words ending with ‘big’.
11. Display those words which are less than 4 characters.
12. Display words with number of characters in each word.
13. Display words with number of vowels and consonants in each word.
14.Display the word which has maximum number of vowels.
14. Display the word which has maximum characters.
15. Display the word which has minimum characters.
16. Display each word separated by a (#).

x=input("enter string")

f = open("text.txt","w")
f.write(x)

f.close()

f = open("text.txt","r")
y = f.read()
f.close()

counts=0
for i in y:
if i == " ":
counts+=1

twords = counts+1
spl=y.split()

countis=0
counta=0
counts_=0
countthe=0
for i in spl:
if i.lower() == "is":
countis+=1
elif i[0]=="a":
counta+=1
elif i[0]=="s":
counts_+=1
elif i[0:4]=="the":
countthe+=1
elif len(i)>=4:
print(i)

print("is =", countis,"a =", counta,"s =",counts,"the =",countthe)


for i in spl:
print(i, len(i))

vowel=0
consonant=0
for i in spl:
for k in i:
if k.lower() in "aeiou":
vowel+=1
else:
consonant+=1
print(i,"vowel = ", vowel, " consonant = ", consonant)
17. Count number of lines.
18. Count number of lines starting with ‘t’.
19. Count number of lines ending with ‘y’.
20. Count the number of lines starting with ‘they’.
21. Count the number of lines ending with ‘free’.
22. Write code to print just the last line.
23.Write code to print just the last line.

f = open("txt2.txt","w")
f.write("hello\n")
f.write("why\n")
f.write("python\n")
f.write("tiger\n")
f.write("py\n")
f.write("theybe\n")
f.close()

f=open("txt2.txt","r")
length = len(f.readlines())
f.close()

f=open("txt2.txt","r")
print(f.readlines())
f.close()

f=open("txt2.txt","r")
count=0
count_t=0
count_y=0
count_they=0
for line in f.readlines():
if line[-3]=="y":
count_y+=1
print(line)
elif line[0]=="t":
count_t+=1
elif line[0:5]=="they":
count_they+=1

f.close()
print("no of lines = ",length)
print("no of line with t = ", count_t)
print("no of line with y = ", count_y)
print("no of line with they = ", count_they)
23. Write a program to take the details (title and price) of book
from the user and write the record in text file.

d1={}
ch= True

while ch:
title=input("enter book title")
price=int(input("enter price"))
d1[title]=price
ch2=input("enter y or n")
if ch2.lower()=="n":
ch=False

f=open("txt3.txt","w")
for i,j in d1.items():
f.write(i + " ")
f.write(str(j)+"\n")
f.close()

f=open("txt3.txt","r")
print(f.read())
f.close()
24. Write a program to take the details (title and price) of book
from the user and write the record in the end of the text file.

d1={}
ch= True

while ch:
title=input("enter book title")
price=int(input("enter price"))
d1[title]=price
ch2=input("enter y or n")
if ch2.lower()=="n":
ch=False

f=open("txt3.txt","a")
for i,j in d1.items():
f.write(i + " ")
f.write(str(j)+"\n")
f.close()

f=open("txt3.txt","r")
print(f.read())
f.close()

You might also like