i am a student working on a project for a python class. i am trying to take one line of a text file at a time and put it into a temp set, then take that temp set and make it a dictionary. then go through the next line so on and so forth. whenever i try to do that it takes the whole file and puts it into a set. im not really sure how to implement my above strategy. right now im just focusing on making everything go through set process. im not yet dealing with the dictionaries. thanks for the help
Complete noob question, sorry
Collapse
X
-
Tags: None
-
Originally posted by Eyes Of Madnessi am a student working on a project for a python class. i am trying to take one line of a text file at a time and put it into a temp set, then take that temp set and make it a dictionary. then go through the next line so on and so forth. whenever i try to do that it takes the whole file and puts it into a set. im not really sure how to implement my above strategy. right now im just focusing on making everything go through set process. im not yet dealing with the dictionaries. thanks for the help
The most common way to go line by line through a text file (as far as I know) is:
[CODE=python]
fh = open('psd_sldr. gm', 'r')
for line in fh:
print line
fh.close()
[/CODE] -
um yeah.
i got it opening the file and as it goes through the line makes everything lower case and takes out punctuation.
Code:import string movieFile = open('movies.txt') tempSet = set() for lines in movieFile: line = line.strip() movieList = line.split() for words in movieList: words = words.lower() words = words.strip(string.punctuation) if words: tempSet.add(words) print tempSet movieFile.close()
Comment
-
Originally posted by Eyes Of Madnessum yeah.
i got it opening the file and as it goes through the line makes everything lower case and takes out punctuation.
Code:import string movieFile = open('movies.txt') tempSet = set() for lines in movieFile: line = line.strip() movieList = line.split() for words in movieList: words = words.lower() words = words.strip(string.punctuation) if words: tempSet.add(words) print tempSet movieFile.close()
Comment
-
thanks for those tips. i actually found some of my problems with it now. the dictionary is being called in later. the way our project works is we have to take this file of actors and the movies they have played in and set up a dictionary. the key is the movie name with the value being the actors or actresses as a set. we are to prompt the user for two movie names to be called into play that tell us what actors were in the movie, what actors werent, and actors that are common to both. the dictionaries are supposed to make this easier i guess.Comment
Comment