Complete noob question, sorry

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Eyes Of Madness
    New Member
    • Mar 2008
    • 5

    Complete noob question, sorry

    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
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by Eyes Of Madness
    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
    Can you provide an example of code that you've tried that didn't work?

    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]

    Comment

    • Eyes Of Madness
      New Member
      • Mar 2008
      • 5

      #3
      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()
      like i said im just trying right now to get it to read only one line at a time and not the whole file

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by Eyes Of Madness
        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()
        like i said im just trying right now to get it to read only one line at a time and not the whole file
        It would be more appropriate to use the variable name 'word' instead of 'words' because you are iterating on a list of words. Also, 'for lines in movieFile:' should be 'for line in movieFile:'. What do you plan to do with the dictionary?

        Comment

        • Eyes Of Madness
          New Member
          • Mar 2008
          • 5

          #5
          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

          Working...