By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,826 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,826 IT Pros & Developers. It's quick & easy.

File to Dictionary?

P: 9
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:\"
  9.  
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!
Jul 1 '08 #1
Share this Question
Share on Google+
6 Replies

jlm699
100+
P: 314
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. >>> 
Jul 1 '08 #2

P: 9
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?
Jul 1 '08 #3

bvdet
Expert Mod 2.5K+
P: 2,851
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'}
Jul 2 '08 #4

P: 9
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
Jul 2 '08 #5

bvdet
Expert Mod 2.5K+
P: 2,851
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]
Jul 2 '08 #6

P: 9
I think it works. Thank you very, very much!
Jul 2 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.