Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

File to Dictionary?

Question posted by: Dekudude (Newbie) on July 1st, 2008 04:47 PM
Hi there, everyone! I was wondering... How would I go about writing a script that creates a dictionary from a file?

EXAMPLE FILE:

Expand|Select|Wrap|Line Numbers
  1. google
  2. start http://www.google.com
  3.  
  4. yahoo
  5. start http://www.yahoo.com
  6.  
  7. c drive
  8. start "C:\"


I want to turn a file like that into a dictionary. Example:

Expand|Select|Wrap|Line Numbers
  1. {'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's Avatar
jlm699
Needs Regular Fix
313 Posts
July 1st, 2008
05:20 PM
#2

Re: File to Dictionary?
Something like this would work...

Expand|Select|Wrap|Line Numbers
  1. >>> f = open('fhtodict', 'w')
  2. >>> f.write('google\nstart http://www.google.com\n\nyahoo\nstart http://www.yahoo.com\n\nc drive\nstart "C:\\"\n')
  3. >>> f.close()
  4. >>> fd = {}
  5. >>> f = open('fhtodict', 'r')
  6. >>> for line in f:
  7. ...     if line[ : 5 ] != 'start' and not line.isspace():
  8. ...         key = line.strip()
  9. ...         fd[ key ] = ''
  10. ...         line = f.next()
  11. ...         fd[ key ] = line[ line.find( 'start' ) + len( 'start' ) + 1 : ].strip()
  12. ...     
  13. >>> fd
  14. {'google': 'http://www.google.com', 'c drive': '"C:\\"', 'yahoo': 'http://www.yahoo.com'}
  15. >>> 

Reply
Dekudude's Avatar
Dekudude
Newbie
9 Posts
July 1st, 2008
06:00 PM
#3

Re: File to Dictionary?
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?

Reply
bvdet's Avatar
bvdet
Expert
1,139 Posts
July 2nd, 2008
12:18 AM
#4

Re: File to Dictionary?
Here's another example:
Expand|Select|Wrap|Line Numbers
  1. s = '''
  2. google
  3. start http://www.google.com
  4.  
  5. yahoo
  6. start http://www.yahoo.com
  7.  
  8. c drive
  9. start "C:\"'''
  10.  
  11. sList = [item for item in s.split('\n') if item]
  12. dd = {}
  13.  
  14. while sList:
  15.     key = sList.pop(0)
  16.     dd[key] = sList.pop(0).split(' ')[-1]
  17.  
  18. print dd    
Output:
>>> {'google': 'http://www.google.com', 'c drive': '"C:"', 'yahoo': 'http://www.yahoo.com'}

Reply
Dekudude's Avatar
Dekudude
Newbie
9 Posts
July 2nd, 2008
01:42 AM
#5

Re: File to Dictionary?
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

Reply
bvdet's Avatar
bvdet
Expert
1,139 Posts
July 2nd, 2008
02:25 AM
#6

Re: File to Dictionary?
Quote:
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:
Expand|Select|Wrap|Line Numbers
  1. sList = [item for item in [line.strip() for line in open(file_name)] if item]

Reply
Dekudude's Avatar
Dekudude
Newbie
9 Posts
July 2nd, 2008
03:17 AM
#7

Re: File to Dictionary?
I think it works. Thank you very, very much!

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,071 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Python Forum Contributors