In [8]:
hfile = open('test.txt')
print(hfile.read())
This is a test
In [9]:
xfile = open('mbox.txt')
count = 0
for line in xfile :
count = count + 1
print (count)
132044
In [39]:
number = 0
count = 0
xfile = open('mbox-short.txt')
for line in xfile :
if line.startswith('X-DSPAM-Confidence') :
position = line.find(':')
number = number + float(line[position + 1 :])
count = count + 1
print('Average Spam Confidence:',float(number)/float(count))
Average Spam Confidence: 0.7507185185185187
In [40]:
xfile = open('mbox-short.txt')
for line in xfile :
line = line.rstrip()
if not line.startswith('From:') :
continue
if not '@uct.ac.za' in line :
continue
print (line)
From: [email protected] From: [email protected] From: [email protected] From: [email protected] From: [email protected] From: [email protected]
In [32]:
i = 0
friends = ['jose', 'steve', 'allan']
for friend in friends :
print ("Hello:", friend.upper())
for i in range(len(friends)) :
x = friends[i]
i = i + 1
print ("Hello", friend)
Hello: JOSE Hello: STEVE Hello: ALLAN Hello allan Hello allan Hello allan
In [49]:
stuff = list()
stuff.append('book')
stuff.append('99')
print(stuff)
stuff.append('cookie')
print(stuff)
if 'cookie' in stuff : print('monster')
stuff.sort()
print(stuff)
['book', '99'] ['book', '99', 'cookie'] monster ['99', 'book', 'cookie']
In [50]:
numlist = list()
while True :
inp = input("Enter a number:")
if inp == 'done' : break
value = float(inp)
numlist.append(value)
average = sum(numlist) / len(numlist)
print('Average', average)
Average 3.6666666666666665
In [54]:
words = 'His e-mail is [email protected]'
print (words)
pieces = words.split()
print (pieces)
parts = pieces[3].split('-')
print (parts)
n = parts [1]
print (n)
His e-mail is [email protected] ['His', 'e-mail', 'is', '[email protected]'] ['q', '[email protected]'] [email protected]
In [94]:
fname = input('Which File?')
fopen = open(fname)
wlist = list()
count = 0
read = fopen.read().split()
for read[count] in read :
if read[count] not in wlist :
wlist.append(read[count])
count = count + 1
wlist.sort()
print(wlist)
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']
In [106]:
fname = input("Enter file name: ")
if len(fname) < 1:
fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh :
if line.startswith("From:") :
sline = line.split(' ')
print(sline[1].strip())
count = count + 1
print("There were", count, "lines in the file with From as the first word")
[email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] There were 27 lines in the file with From as the first word
In [ ]: