473,395 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

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
6 1362
jlm699
314 100+
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
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
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
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
I think it works. Thank you very, very much!
Jul 2 '08 #7

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

Similar topics

4
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply*...
10
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, my program builds a dictionary that I would like to save in a file. My question is what are the simple ways to do it? The first solution...
4
by: Julian Yap | last post by:
Hi all, I'm trying to get some ideas on the best way to do this. In this particular coding snippet, I was thinking of creating a dictionary of file objects and file names. These would be...
1
by: laredotornado | last post by:
Hello, I want to play an audio file embedded on my page by clicking on an audio image and the page change to a new page. Is there a cross-browser Javascript way to do this? Right now the code I...
0
by: laredotornado | last post by:
Hello, I want to play an audio file by clicking on an audio icon and not having the page switch out underneath. Right now the code I have is ... <html> <head> <title>Dictionary:...
12
by: teoryn | last post by:
I've been spending today learning python and as an exercise I've ported a program I wrote in java that unscrambles a word. Before describing the problem, here's the code: *--beginning of file--*...
5
by: rohit | last post by:
i want to implement a dictionary in python the query is : without explicitly doing the following steps 1. reading from file to list 2. processing list to dictionary is there a module or a built...
3
GTXY20
by: GTXY20 | last post by:
Hi All, I have been able to create a successful application with Python but I am being asked to create in VB. I have a text file with the following data: 1,a 1,b 1,c 2,a
0
GTXY20
by: GTXY20 | last post by:
Hi all, I currently have a CSV file like so 1,a 1,b 1,c 2,a 2,b 2,b
1
by: jim-on-linux | last post by:
On Saturday 14 June 2008 03:15, Beema shafreen wrote: I have used this method to solve similar problems. This is a consept of how to do what you want, but you will have to work a little to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.