0% found this document useful (0 votes)
23 views2 pages

Untitled 15

The document contains code snippets in Python that demonstrate: 1) Reading a text file and printing lines that start with a specific word. 2) Counting the occurrences of each vowel in a file by loading data with pickle and iterating over lines. 3) Writing to and reading from a CSV file by using the CSV module to write and read rows. 4) Taking user input and writing it to a binary file using pickle dump.

Uploaded by

FF gaming
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)
23 views2 pages

Untitled 15

The document contains code snippets in Python that demonstrate: 1) Reading a text file and printing lines that start with a specific word. 2) Counting the occurrences of each vowel in a file by loading data with pickle and iterating over lines. 3) Writing to and reading from a CSV file by using the CSV module to write and read rows. 4) Taking user input and writing it to a binary file using pickle dump.

Uploaded by

FF gaming
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/ 2

In [ ]: #Write a function in Python to read a text file, Alpha.

txt and displays those lines which begin with the word ‘You’.
with open("Alpha.txt","r") as file:
lines=file.readlines()
for line in lines:
words=line.split()
if word[0]=="You":
print(line) #DISPLAYING THE LINE STARTING WITH "YOU"

In [ ]: #find the number of occurence of each vowel in the file.


import pickle
with open("File_name.dat","rb") as file:
lines=[]
while True:
try:
line=pickle.load(file)
lines.append(line) #STORING THE DATA IN A LIST
except EOFError:
break
number_of_a=0
number_of_e=0
number_of_i=0
number_of_o=0
number_of_u=0
for line in lines:
for letter in line:
if letter.upper()=="A":
number_of_a+=1 #COUNTING THE OCCURANCE OF THE VOWEL
if letter.upper()=="E":
number_of_e+=1
if letter.upper()=="I":
number_of_i+=1
if letter.upper()=="O":
number_of_o+=1
if letter.upper()=="U":
number_of_u+=1
print("Number of A's in the file is/are: ", number_of_a)
print("Number of E's in the file is/are: ", number_of_e)
print("Number of I's in the file is/are: ", number_of_i)
print("Number of O's in the file is/are: ", number_of_o)
print("Number of U's in the file is/are: ", number_of_u)
In [ ]: #writing and reading a CSV file
import csv
with open("File_name.csv","w") as file:
writing=csv.writer() #MAKING A WRITER
writing.writerow(["Dhruv",17]) #ADDING DATA
writing.writerow(["Harshit",17])
writing.writerow(["Anuj",17])
with open("File_name.csv","r") as file:
reading=csv.reader() #MAKING A READER
for i in reading:
print(i) #DISPLAYING ALL THE DATA ADDED

In [ ]: #writing in a binary file


import pickle
with open("File_name.dat","wb") as file:
for i in range(5):
line=input("Enter: ") #TAKING INPUT
pickle.dump(line,file) #pickle.dump(data you want to write, file name)

You might also like