google.com, pub-7659628848972808, DIRECT, f08c47fec0942fa0 Pro Learner: python codes for file handling and college projects..

Please click on ads

Please click on ads

Saturday 17 April 2021

python codes for file handling and college projects..

 # fname = input("Enter file name: ")

# fh = open(fname)
# lst=[]
#
# for line in fh:
# if not line.startswith("X-DSPAM-Confidence:") : continue
# b=line
# #print(b)
# x=b.find('0')
# host=b[x:]
# #print(host)
# flt=float(host)
# #print(flt)
# lst.append(flt)
# #print(lst)
#
# total=0
# count=0
# for i in lst:
# total=i+total
# count=count+1
# #print(count)
# print("Average spam confidence:",total/count)





# fname = input("Enter file name: ")
# fopen = open(fname)
# count = 0
# average = 0
# for line in fopen:
# if not line.startswith("X-DSPAM-Confidence:") :continue
# average += float(line[20:-1].strip())
# count = count + 1
# #print(line)
#
# print("Average spam confidence:", (average/count))
#





# name = input("Enter file:")
# fopen = open(name)
# if len(name) < 1 :
# name = "mail.txt"
#
# d=dict()
# for line in fopen:
# if not line.startswith("From "):
# continue
# else:
# line=line.split()
# line=line[5]
# line=line[0:2]
# d[line]=d.get(line,0)+1
#
# lst=list()
# for value,count in d.items():
# lst.append((value,count))
#
# lst.sort()
# for value,count in lst:
# print (value,count)



# name = input("Enter file name:")
# if len(name) < 1:
# name = "mail.txt"
# fopen = open(name)
# l=list()
# count=dict()
# for line in fopen :
# if line.startswith("From "):
# line=line.strip().split()
# hours=line[5]
# hours=hours[:2]
# l.append(hours)
#
# for hour in l :
# count[hour]=count.get(hour,0)+1
#
# for k,v in sorted(count.items()) :
# print(k,v)

fname = input("Enter file:")
if len(fname) < 1 : name = "mail.txt"
hand = open(fname)

lst = list()

for line in hand:
if not line.startswith("From:"): continue
line = line.split()
lst.append(line[1])

counts = dict()
for word in lst:
counts[word] = counts.get(word,0) + 1

bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigcount = count
bigword = word

print (bigword,bigcount)




# import re
# file = input('enter the file name') #assumed input file
# fopen=open(file)
# for line in fopen:
# x=re.search('^From.*[0-9][0-9]:.x', line) #applied regular expression
# if len(x) > 0:
# print(x)




# Exercise 2: Write a program to look for lines of the form

# New Revision: 39772

#and extract the number from each of the lines using a regular
# expression and the findall() method. Compute the average of the numbers and print out
# the average.

# Enter file:mbox.txt 38549.7949721

# Enter file:mbox-short.txt 39756.9259259

# import re
# count = 0
# revisions = 0
# #Opening the file so that we can search through it
# file = input('enter the file name')
#
# fopen=open(file)
# for line in fopen:
# line = line.rstrip()
# if re.findall('New Revision:', line):
# revison = line[14:19]
# count += 1
# #We'll count each instance and add each new found one to the next to make the sum
# revisions = revisions + float(revison)
#
# average = revisions/count
# print("the average of the numbers in the file mail.txt is " + str(average))




# hour = input("Enter Hours:")
# ihour = float(hour)
# rate = input("Enter the Rate:")
# irate = float(rate)
# if ihour <= 40:
# print( ihour * irate)
# elif ihour > 40:
# print(40* irate + (ihour-40)*1.5*irate)



# first create a list

#
# # # Program without using any external library
# s = open('mail.txt').read()
# l = s.split()
# k = []
# for i in l:
#
# # If condition is used to store unique string
# # in another list 'k'
# if (s.count(i)>1 and (i not in k)or s.count(i)==1):
# k.append(i)
# print(' '.join(k))
# print(len(k))


# from collections import Counter
# p=[]
# def remov_duplicates(input):
#
# # split input string separated by space
# input = input.split(" ")
#
# # joins two adjacent elements in iterable way
# for i in range(0, len(input)):
# input[i] = "".join(input[i])
#
# # now create dictionary using counter method
# # which will have strings as key and their
# # frequencies as value
# UniqW = Counter(input)
#
# # joins two adjacent elements in iterable way
# s = " ".join(UniqW.keys())
# print (s)
#
# # Driver program
# if __name__ == "__main__":
# input = open('mail.txt').read()
# remov_duplicates(input)




#6
# number = input("Enter the number ")
# s = float(number)
# x = 0
#
# if s >= 0.9:
# x = 'A'
# elif s >=0.8:
# x='B'
# elif s >=0.7:
# x='C'
# elif s >= 0.6:
# x='D'
# elif s < .6:
# x ='F'
# else:
# x ="Out of Range"
# print (x)

# s = open('mail.txt').read()
# l = s.split()
# k = list()
# p=[]
# for i in l:
#
# # If condition is used to store unique string
# # in another list 'k'
# if (s.count(i)>1 and (i not in k)or s.count(i)==l):
# k.append(i)
# print(' '.join(k))
# print(len(l))
# print(len(k))

No comments:

Post a Comment