473,388 Members | 1,066 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,388 software developers and data experts.

Naming dictionaries recursively

TYR
I'd like to do something like this; iterate through a file which
consists of data stored in dictionary format, one dict on each line,
and read each line into a new dict using one of the values in the dict
as its name...

for example:

stuff = open('data.txt')
for eachLine in stuff:
name{}
name = eachLine
.....and then do something clever to extract the value of the key
(name) from the line and use it as the dictionary's name.

A line from data.txt would look like this: {'name' : Bob, 'species' :
Humboldt, 'colour' : red, 'habits' : predatory}. Aim is to call one of
them by name, and merge the values in that dictionary into a string
pulled from another source.

Aug 17 '07 #1
7 1367
On Aug 17, 7:38 am, TYR <a.harrow...@gmail.comwrote:
I'd like to do something like this; iterate through a file which
consists of data stored in dictionary format, one dict on each line,
and read each line into a new dict using one of the values in the dict
as its name...

for example:

stuff = open('data.txt')
for eachLine in stuff:
name{}
name = eachLine
....and then do something clever to extract the value of the key
(name) from the line and use it as the dictionary's name.

A line from data.txt would look like this: {'name' : Bob, 'species' :
Humboldt, 'colour' : red, 'habits' : predatory}. Aim is to call one of
them by name, and merge the values in that dictionary into a string
pulled from another source.
I'm not sure I follow exactly what you want to do, but you can always
use eval for each line in that file.

But, if the line you provided for testing is one that actually comes
from the file, you'll have to patch it before you eval the line. I
think this regexp will work. Be careful though, it assumes that all
values are whole words, that is they don't have spaces in them.

# This is far from ideal, but you get what you pay for :).
re.sub(r':\s*(\w+)(,|})', r"': '\1'\2", line)

Anyway, after you've cleaned up your input line this ought to work:
d = eval(line)

Also, if you're building the input file from within a python program,
maybe you should consider the pickle module.

That ought to give you a good start...

jw

Aug 17 '07 #2
On Fri, 17 Aug 2007 12:38:16 +0000, TYR wrote:
I'd like to do something like this; iterate through a file which
consists of data stored in dictionary format, one dict on each line,
and read each line into a new dict using one of the values in the dict
as its name...
Store the dictionaries in a dictionary with that value as key.
A line from data.txt would look like this: {'name' : Bob, 'species' :
Humboldt, 'colour' : red, 'habits' : predatory}. Aim is to call one of
them by name, and merge the values in that dictionary into a string
pulled from another source.
So the tougher problem seems to be parsing those lines. That is not a
valid Python dictionary unless the names `Bob`, `Humboldt`, `red`, and
`predatory` are not already defined. So you can't just ``eval`` it.

Ciao,
Marc 'BlackJack' Rintsch
Aug 17 '07 #3
TYR
So the tougher problem seems to be parsing those lines. That is not a
valid Python dictionary unless the names `Bob`, `Humboldt`, `red`, and
`predatory` are not already defined. So you can't just ``eval`` it.
In what way? {'key': val}, right?

Anyway, I can always change the format they go into the file in.


Aug 17 '07 #4
On Aug 17, 7:38 am, TYR <a.harrow...@gmail.comwrote:
I'd like to do something like this; iterate through a file which
consists of data stored in dictionary format, one dict on each line,
and read each line into a new dict using one of the values in the dict
as its name...

for example:

stuff = open('data.txt')
for eachLine in stuff:
name{}
name = eachLine
....and then do something clever to extract the value of the key
(name) from the line and use it as the dictionary's name.

A line from data.txt would look like this: {'name' : Bob, 'species' :
Humboldt, 'colour' : red, 'habits' : predatory}. Aim is to call one of
them by name, and merge the values in that dictionary into a string
pulled from another source.
Pyparsing includes an example that is very similar to this. Here is
that example adapted to your specific data:

from pyparsing import *

line = """{'name' : Bob, 'species' : Humboldt, 'colour' : red,
'habits' : predatory}"""

LBRACE,RBRACE,COLON,COMMA = map(Suppress,"{}:,")
key = sglQuotedString.setParseAction(removeQuotes)
value = OneOrMore(Word(alphanums))\
.setParseAction(keepOriginalText)
entry = Group(key + COLON + empty + value)
lineExpr = LBRACE + Dict(delimitedList(entry)) + RBRACE

parsedData = lineExpr.parseString(line)

# some examples of accessing the parsed data
print "Keys:", parsedData.keys()
print parsedData.name
print parsedData.colour
print "Name: %(name)s \nSpecies: %(species)s \n" \
"Colour: %(colour)s \nHabits: %(habits)s" % parsedData

Prints:

Keys: ['colour', 'habits', 'name', 'species']
Bob
red
Name: Bob
Species: Humboldt
Colour: red
Habits: predatory

-- Paul

Aug 17 '07 #5
On Aug 17, 3:43 pm, TYR <a.harrow...@gmail.comwrote:
So the tougher problem seems to be parsing those lines. That is not a
valid Python dictionary unless the names `Bob`, `Humboldt`, `red`, and
`predatory` are not already defined. So you can't just ``eval`` it.

In what way? {'key': val}, right?

Anyway, I can always change the format they go into the file in.
If you control the file format then you could create a valid python
list of dictionaries as the intermediate file format. Something like:

data = [
{'name' : 'Bob', 'species' : 'Humboldt', 'colour' : 'red',
'habits' : 'predatory'},
{ ... },
...
}

You can then name the file with a .py ending and import it as a list
of dictionaries that you can then process to form a dict of dicts:

datadict = dict( (data2name(d), d) for d in modulename.data )

- Paddy.

Aug 17 '07 #6
TYR
That sounds like a solution. I think the core of the question is as
follows; if I was to call the dict() function for each line, thus
creating a dictionary, and then rename it to dict['name'], will there
be a namespace collision on the next loop when dict() is called again?
Obviously the first dictionary will still be there, as it still has a
refcount of 1; but if it gets overwritten, will the object that the
new name refers to be overwritten too?

Aug 18 '07 #7
On Aug 18, 11:44 am, TYR <a.harrow...@gmail.comwrote:
That sounds like a solution. I think the core of the question is as
follows; if I was to call the dict() function for each line, thus
creating a dictionary, and then rename it to dict['name'], will there
be a namespace collision on the next loop when dict() is called again?
Obviously the first dictionary will still be there, as it still has a
refcount of 1; but if it gets overwritten, will the object that the
new name refers to be overwritten too?
if data2name might give colliding names then you could use tuples as
dictionary keys as shown below:

import modulename
datadict = dict( ((data2name(d), count), d)
for count,d in enumerate(modulename.data) )

Generally, if you try and create two keys with the same name in a
dictionary, they will clash and only the second value is retained.

- Paddy.

Aug 18 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
8
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
3
by: Shivram U | last post by:
Hi, I want to store dictionaries on disk. I had a look at a few modules like bsddb, shelve etc. However would it be possible for me to do the following hash = where the key is an int and not...
8
by: beliavsky | last post by:
Since Python does not have declarations, I wonder if people think it is good to name function arguments according to the type of data structure expected, with names like "xlist" or "xdict".
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
5
by: aurora00 | last post by:
I use generators a lot. E.g. def gen_words(text) ... parse text ... yield each word in text for word in gen_words(text): print word
23
by: Thorsten Kampe | last post by:
Okay, I hear you saying 'not another naming conventions thread'. I've read through Google and the 'naming conventions' threads were rather *spelling conventions* threads. I'm not interested...
0
by: d80013 | last post by:
Hello all, I am trying to create a Dictionary of dictionaries in VBA. All I do is declare two dictionaries, one temporary one, and add the temporary dictionary to the main one recursively. The...
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.