I know that this is an easy one but it is hard for me to figure out.
I need to make a program that can:
* Open the file and read through all of the lines in the file. (got this)
* If a line starts with "Subject:", skip the line. (got this)
* For all the other lines, split the strings into a list of words
using the white space and find the first word. After you have
found the first word, split that string again into a *new* list of
words using the "/" character. (got this I think with the words=line.split())
* Look at the second word of the *new* list. If it does not contain
"trunk" or "branches", skip the line. (stuck on this)
* For the lines we still have left, look at the first word of the
*new* list, and use that word to index your dictionary of running
counts for each module. (stuck on this)
* At the end of the program, print out your dictionary of counts
What I don't understand is how to split the line a second time to look at the second word that the above guideline is referring to. That is stopping me from even beginning the search for the words 'branches' and 'trunk'. Here is the code that I have written so far:
file = raw_input('Enter a file name: ')
try:
fhand = open(file)
except:
'file cannot be opened:', file
counts=dict()
for line in fhand:
if line.startswith('Subject:'):
continue
else:
words=line.split()
modified=words[0]
if len(words) == 0 : continue
modified=modified.split('/')
x=modified[1]
if len(modified) == 0 : continue
if x not in counts:
counts[x]=1
else:
counts[x] = counts[x]+1
lst = list()
for val, key in counts.items():
lst.append( (val, key) )
lst.sort()
for val, key in lst[:] :
print val, key
I would greatly appreciate any advice. Thank you in advance!