File to Dictionary?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dekudude
    New Member
    • Jul 2008
    • 9

    File to Dictionary?

    Hi there, everyone! I was wondering... How would I go about writing a script that creates a dictionary from a file?

    EXAMPLE FILE:

    Code:
    google
    start http://www.google.com
    
    yahoo
    start http://www.yahoo.com
    
    c drive
    start "C:\"
    I want to turn a file like that into a dictionary. Example:

    Code:
    {'google': 'start http://www.google.com', 'yahoo': 'start http://www.yahoo.com', 'c drive': 'start "C:\"'}
    How would I do this? Thanks a lot for your help!
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Something like this would work...

    [code=python]
    >>> f = open('fhtodict' , 'w')
    >>> f.write('google \nstart http://www.google.com\ n\nyahoo\nstart http://www.yahoo.com\n \nc drive\nstart "C:\\"\n')
    >>> f.close()
    >>> fd = {}
    >>> f = open('fhtodict' , 'r')
    >>> for line in f:
    ... if line[ : 5 ] != 'start' and not line.isspace():
    ... key = line.strip()
    ... fd[ key ] = ''
    ... line = f.next()
    ... fd[ key ] = line[ line.find( 'start' ) + len( 'start' ) + 1 : ].strip()
    ...
    >>> fd
    {'google': 'http://www.google.com' , 'c drive': '"C:\\"', 'yahoo': 'http://www.yahoo.com'}
    >>> [/code]

    Comment

    • Dekudude
      New Member
      • Jul 2008
      • 9

      #3
      Thank you! However, that's not exactly what I want. The file was just an example. The second line of each "block" won't always start with "start"... it just will sometimes. I need it to basically search for the line breaks, and split that apart, not the terms.

      Make sense?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Here's another example:[code=Python]s = '''
        google
        start http://www.google.com

        yahoo
        start http://www.yahoo.com

        c drive
        start "C:\"'''

        sList = [item for item in s.split('\n') if item]
        dd = {}

        while sList:
        key = sList.pop(0)
        dd[key] = sList.pop(0).sp lit(' ')[-1]

        print dd [/code]Output:
        >>> {'google': 'http://www.google.com' , 'c drive': '"C:"', 'yahoo': 'http://www.yahoo.com'}

        Comment

        • Dekudude
          New Member
          • Jul 2008
          • 9

          #5
          Yours works, but how do I read from an external file? This only works if the commands are built directly into the script, which I don't want. :P

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by Dekudude
            Yours works, but how do I read from an external file? This only works if the commands are built directly into the script, which I don't want. :P
            Something like this will read a file and create a list equivalent to my earlier post:
            Code:
            sList = [item for item in [line.strip() for line in open(file_name)] if item]

            Comment

            • Dekudude
              New Member
              • Jul 2008
              • 9

              #7
              I think it works. Thank you very, very much!

              Comment

              Working...