Convert file with information into dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ChaoticBlack
    New Member
    • Apr 2011
    • 3

    Convert file with information into dictionary

    Hello, I have a text file with information on products, the first 3 lines can be seen below
    CODBO, Call of Duty BlackOps, 120
    SC2WL, Starcraft 2 WoL, 90
    FONV0, Fallout New Vegas 100
    CODBO is the ID, Call... is the name and 120 is the price.
    What would the code be for a function that would turn that into a dictionary that looks similarly to this:
    {'CODBO':('Call of Duty BlackOps',120), ...}
    I have tried to think of a solution for 2 hours now and still dont have the faintest idea on how to do it. Any help is appreciated, thanks.

    EDIT:
    Thanks bvdet, i modified the code a bit and got it to work, here it is for reference.
    Code:
    def load_games(filename) :
        games = {}
        f = open(filename, 'U')
        s = ''
        for line in f:
            s = line
            sList = s.split(',')
            games[sList[0]] = sList[1].strip(), int(sList[2]) 
        return games
    
    x = load_games('games.txt')
    print x
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Do you know how to open a file, read the file or iterate on the file object, and close the file? The following code will parse one of the lines and enter the information into a dictionary.
    Code:
    >>> dd = {}
    >>> s = "CODBO,  Call of Duty BlackOps,      120"
    >>> sList = s.split(",")
    >>> dd[sList[0]] = sList[1].strip(), int(sList[2])
    >>> dd
    {'CODBO': ('Call of Duty BlackOps', 120)}
    >>>

    Comment

    • ChaoticBlack
      New Member
      • Apr 2011
      • 3

      #3
      Yeh I know most of the basics with dictionaries and files, that code helps a lot, Ill try to include it in a for loop and iterate over every line. Ill post the code soon if it doesn't work. Thanks
      Last edited by ChaoticBlack; Apr 6 '11, 01:55 AM.

      Comment

      • ChaoticBlack
        New Member
        • Apr 2011
        • 3

        #4
        So far I can create a list with the games (code below), how can I convert it to a dictionary in the style {'CODBO':('Call of Duty BlackOps',120), ...}?
        Code:
        def load_games(filename) :
            games = {}
            f = open(filename, 'U')
            s = ''
            for line in f:
                s += line
            sList = s.split(',')
            return sList
        
        x = load_games('games.txt')
        print x
        Has the output
        Code:
        >>> 
        ['CODBO', ' Call of Duty BlackOps', ' 120\nSC2WL', ' Starcraft 2 WoL', ' 90\nFONV0', ' Fallout New Vegas', ' 100']
        >>>

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Like this (untested):
          Code:
          def load_games(filename) :
              games = {}
              f = open(filename)
              for line in f:
                  sList = s.strip().split(",")
                  games[sList[0]] = sList[1].strip(), int(sList[2])
              f.close()
              return sList
           
          x = load_games('games.txt')
          print x

          Comment

          Working...