Write a method/function COUNTLINES_ET() in python to read lines from a
text file REPORT.TXT, and COUNT those lines which are starting either with
‘E’ and starting with ‘T’ respectively. And display the Total count separately.
Answers
Answer:
def COUNTLINES_ET():
f=open(“REPORT.TXT”)
d=f.readlines()
le=0
lt=0
for i in d:
if i[0]==’E:
le=le+1
elif i[0]==’T’:
lt=lt+1
print(“no of line start with”,le)
print(“no of line start with”,lt)
This will open the file "REPORT.TXT" in the current directory and read each line. It will then check if the line starts with "E" or "T", and increment the appropriate counter. Finally, it will print the count of lines starting with "E", and "T" and the total count of lines in the file.
def COUNTLINES_ET():
e_count = 0
t_count = 0
total_count = 0
with open("REPORT.TXT", "r") as f:
for line in f:
total_count += 1
if line.startswith("E"):
e_count += 1
elif line.startswith("T"):
t_count += 1
print("Lines starting with 'E':", e_count)
print("Lines starting with 'T':", t_count)
print("Total lines:", total_count)
You can call this method like this:
COUNTLINES_ET()
#SPJ3