0% found this document useful (0 votes)
7 views4 pages

App Lab Expt 6

The document contains Python code snippets for various tasks including reading and analyzing text files, plotting histograms, and extracting hidden messages from images. It demonstrates how to count characters, words, and lines in a file, find words starting with vowels, and visualize word lengths. Additionally, it includes a method for hiding and revealing messages in images using the PIL library.

Uploaded by

prabhakarele8
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)
7 views4 pages

App Lab Expt 6

The document contains Python code snippets for various tasks including reading and analyzing text files, plotting histograms, and extracting hidden messages from images. It demonstrates how to count characters, words, and lines in a file, find words starting with vowels, and visualize word lengths. Additionally, it includes a method for hiding and revealing messages in images using the PIL library.

Uploaded by

prabhakarele8
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/ 4

x = [1, 1, 2, 3, 3, 5, 7, 8, 9, 10,

10, 11, 11, 13, 13, 15, 16, 17, 18, 18,
18, 19, 20, 21, 21, 23, 24, 24, 25, 25,
25, 25, 26, 26, 26, 27, 27, 27, 27, 27,
29, 30, 30, 31, 33, 34, 34, 34, 35, 36,
36, 37, 37, 38, 38, 39, 40, 41, 41, 42,
43, 44, 45, 45, 46, 47, 48, 48, 49, 50,
51, 52, 53, 54, 55, 55, 56, 57, 58, 60,
61, 63, 64, 65, 66, 68, 70, 71, 72, 74,
75, 77, 81, 83, 84, 87, 89, 90, 90, 91
]

plt.hist(x, bins=5)
plt.show()
output:

6. The strings package

a) Read text from a file and print the number of lines, words and characters

filename = input("Enter a file name : ")


c=0
w=0
l=0
f1 = open(filename,'w')
f1.write('Computer science Engineering\n')
f1.write('Elecronics and Communication Engineering\n')
f1.write('Civil Engineering\n')
f1.close()
f2 = open(filename,'r')
data = f2.readlines()
print(data)
for i in data:
c = c + len(i)
w = w + len(i.split())
l=l+1
f2.close()
print('The number of Characters are :', c)
print('The number of Words are :', w)
print('The number of Lines are :', l)
output:

D:\Sree>python 6a.py
Enter a file name : file.txt
['Computer science Engineering\n', 'Elecronics and Communication Engineering\n', 'Civil Engineering\n']
The number of Characters are : 88
The number of Words are : 9
The number of Lines are : 3

b) Read text from a file and return a list of all n letter words beginning with a vowel
Program:
file1 = open("file.txt", "r")
data = file1.read()
words = data.split()

list1 = []

for i in words:

if i[0] in "aeiouAEIOU":
list1.append(i)

print(list1)
file1.close()

output:

c. Finding a secret message hidden in a paragraph of text


# txt_hide.py
#txt_show.py
from PIL import Image
import numpy as np
import tkinter.filedialog as tkf
# Get the image file name
root = tkf.Tk()
root.withdraw()
img_file = tkf.askopenfilename( parent=root, title="Select a PNG file", filetypes=(("PNG files", "*.png"),) ) # Load and
reshape the image file
img = np.array(Image.open(img_file))
shp = img.shape
the_bytes = img.reshape(np.prod(shp))
# Extract the message
char_codes, n, c, m = [], 0, 0, 1
for byt in the_bytes:
if byt & 1:
c += m
n += 1
m <<= 1
if n % 8 == 0:
if c == 4:
break
char_codes.append(c)
c, m = 0, 1
s = "".join(map(chr, char_codes))
# Output the results
print(s)

d) Plot a histogram of words according to their length from text read from a file
Program:
from collections import Counter
import numpy as np
import matplotlib.pyplot as plt

full = "hi hello well come to computer science and engineering electronics and communication engineering all the best"
x=list(full)
labels, values = zip(*Counter(x).items())

indexes = np.arange(len(labels))
width = 1

plt.bar(indexes, values, width)


plt.xticks(indexes + width * 0.5, labels)
plt.show()

output:

You might also like