N
nateastle
I have a simple assignment for school but am unsure where to go. The
assignment is to read in a text file, split out the words and say which
line each word appears in alphabetical order. I have the basic outline
of the program done which is:
def Xref(filename):
try:
fp = open(filename, "r")
lines = fp.readlines()
fp.close()
except:
raise "Couldn't read input file \"%s\"" % filename
dict = {}
for line_num in xrange(len(lines)):
if lines[line_num] == "": continue
words = lines[line_num].split()
for word in words:
if not dict.has_key(word):
dict[word] = []
if line_num+1 not in dict[word]:
dict[word].append(line_num+1)
return dict
My question is, how do I easily parse out punction marks and how do I
sort the list and if there anything else that I am doing wrong in this
code it would be much help.
assignment is to read in a text file, split out the words and say which
line each word appears in alphabetical order. I have the basic outline
of the program done which is:
def Xref(filename):
try:
fp = open(filename, "r")
lines = fp.readlines()
fp.close()
except:
raise "Couldn't read input file \"%s\"" % filename
dict = {}
for line_num in xrange(len(lines)):
if lines[line_num] == "": continue
words = lines[line_num].split()
for word in words:
if not dict.has_key(word):
dict[word] = []
if line_num+1 not in dict[word]:
dict[word].append(line_num+1)
return dict
My question is, how do I easily parse out punction marks and how do I
sort the list and if there anything else that I am doing wrong in this
code it would be much help.