CONTEST - What is the (best) solution?

P

python

In a file there can be several dictionaries like this
{Key11: Value11
Key12: Value12
Key13: Value13,
....
....
Key1n:Value1n}
{Key21: Value21
Key22: Value22
Key23: Value23,
....
....
Key2n:Value2n}
{Key31: Value31
Key32: Value32
Key33: Value33,
....
....
Key3n:Value3n}
.....
.....
.....
{Keyn1: Valuen1
Keyn2: Valuen2
Keyn3: Value3,
....
....
Keynn:Valuenn}

Each pair in a dictionary is separated by CRLF and in each dictionary
numbers of pairs can be different.
I need to read only the the first and the last dictionaries.What is a
best solution?
Thanks
Lad
 
F

Fuzzyman

Can your dictionaries contain dictionaries ?

If not you can read the file and cut at the first '}' and the last '{'.
The two chunks will then be a single dictionary which can be eval'd.

*However* your example above does not show ',' between all of the
pairs... but only some. This will bugger you royally (because you can't
even replace '\n' with ','). You'd then need to parse the two chunks
and correct.....
Regards,


Fuzzy
http://www.voidspace.org.uk/python/index.shtml
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
In a file there can be several dictionaries like this (snip)
I need to read only the the first and the last dictionaries.What is a
best solution?

Depends on your definition of 'best solution'.
 
P

python

Fuzzyman said:
Can your dictionaries contain dictionaries ?

If not you can read the file and cut at the first '}' and the last '{'.
The two chunks will then be a single dictionary which can be eval'd.

*However* your example above does not show ',' between all of the
pairs... but only some. This will bugger you royally (because you can't
even replace '\n' with ','). You'd then need to parse the two chunks
and correct.....
Regards,
Hi Fuzzy,
dictionaries can NOT contain dictionaries.
I re-checked and I would need only the last dictionary.
Any idea how I could do that?
Thanks
Lad.
 
F

Fuzzyman

What about the syntax ? Will it have commas in the right place ?
(never, always, or sometimes ? - sometimes is much worse than never or
always).

*damn* - problem is that '{' might appear as one of the values....
*So*... read the file in as a list of lines and strip each line.
Dictionaries will always start where '{' is the first character. Anyway
- you could always explicitly check if each line ends with a ',' and if
it doesn't add one...)
(*warning* UNTESTED)
handle = open('filename', 'r')
thefile = handle.readlines()
handle.close()

dictindex = []
i = 0
while i < len(thefile)-1:
if line.startswith('{'):
dictindex.append(i)
i += 1
lastdict = ' '.join(thefile[dictindex[-1]:]
print eval(lastdict)

Checking if each line ends with a ',' (except for the last line) should
be easy !
HTH

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml
 
L

Larry Bates

Assumptions:
1) You actually meant to have commas between each key value pair
(which your example DOES NOT have).

2) File can be read into memory

3) All the key and value variables are actually defined in the
local namespace or they are literal values instead of references
to variables.

This works:

Key11=11
Value11='V11'
Key12=12
Value12='12'
Key13=13
Value13='V13'
Key21=21
Value21='V21'
Key22=22
Value22='V22'
Key23=32
Value23='V23'
Key31=31
Value31='V31'
Key32=32
Value32='V32'
Key33=33
Value33='V33'

testdata='''
{Key11: Value11,\n
Key12: Value12,\n
Key13: Value13}\n
{Key21: Value21,\n
Key22: Value22,
Key23: Value23}\n
{Key31: Value31,\n
Key32: Value32,\n
Key33: Value33}\n
'''
#
# Or read data from a file
#
# f=open(file, 'r')
# testdata=f.read()
# f.close()
#
dictlist=testdata.replace('\n','').split('{')
firstdictstr='{'+dictlist[2]
lastdictstr='{'+dictlist[-1]

firstdict=eval(firstdictstr)
lastdict=eval(lastdictstr)

print "firstdict=", firstdict
print "lastdict=", lastdict
firstdict=eval(firstdictstr)
lastdict=eval(lastdictstr)

Larry Bates
 
M

Miki Tebeka

Hello Lad,
In a file there can be several dictionaries like this
{Key11: Value11
Key12: Value12
Key13: Value13,
...
...
Key1n:Value1n}
{Key21: Value21
Key22: Value22
Key23: Value23,
...
...
Key2n:Value2n}
{Key31: Value31
Key32: Value32
Key33: Value33,
...
...
Key3n:Value3n}
....
....
....
{Keyn1: Valuen1
Keyn2: Valuen2
Keyn3: Value3,
...
...
Keynn:Valuenn}

Each pair in a dictionary is separated by CRLF and in each dictionary
numbers of pairs can be different.
I need to read only the the first and the last dictionaries.What is a
best solution?

---- d.py ---

#!/usr/bin/env python

from sys import argv

dicts = eval("[" + open(argv[1]).read().replace("}", "},") + "]")
print dicts[0], dicts[-1]

---- d.py ---

HTH.
 
J

Jeremy Bowers

Each pair in a dictionary is separated by CRLF and in each dictionary
numbers of pairs can be different.
I need to read only the the first and the last dictionaries.What is a
best solution?
Thanks
Lad

Who cares about the best solution? Odds are, your process is disk-bound
anyhow.

Is this a thinly-veiled attempt to get someone to provide you *a*
solution, or do you already have one and you are seriously asking for a
better one? Because I'd say, take the easiest programming route: Parse the
first dict into a variable, then just loop until you run out of file,
storing a parsed dict in the "last" variable and just naturally letting
later ones overwrite earlier ones.

If you want something "best"-er than that (heh heh), you're going to have
to tell us how you are measuring better-ness.
 
S

Steven Bethard

In a file there can be several dictionaries like this
{Key11: Value11
Key12: Value12
Key13: Value13,
...
...
Key1n:Value1n}
{Key21: Value21
Key22: Value22
Key23: Value23,
...
...
Key2n:Value2n}
{Key31: Value31
Key32: Value32
Key33: Value33,
...
...
Key3n:Value3n}
....
....
....
{Keyn1: Valuen1
Keyn2: Valuen2
Keyn3: Value3,
...
...
Keynn:Valuenn}

Each pair in a dictionary is separated by CRLF and in each dictionary
numbers of pairs can be different.
I need to read only the the first and the last dictionaries.What is a
best solution?

Assumption:
File is small enough to be read into memory at once. Contents of the
file have been read into the variable "testdata":

py> # your data
py> testdata="""
.... {Key11: Value11
.... Key12: Value12,
.... Key13: Value13}
.... {Key21: Value21,
.... Key22: Value22
.... Key23: Value23}
.... {Key31: Value31
.... Key32: Value32,
.... Key33: Value33}
.... """
py>
py> # get the contents of the first and last dict
py> content_strs = testdata.strip('{}\n\t ').split('}\n{')
py> content_strs = content_strs[0], content_strs[1]
py> print content_strs
('Key11: Value11\nKey12: Value12,\nKey13: Value13', 'Key21:
Value21,\nKey22: Value22\nKey23: Value23')
py>
py> # correct missing commas and add braces
py> dict_strs = ['{%s}' % s.replace('\n', ',\n').replace(',,\n', ',\n')
.... for s in content_strs]
py> print dict_strs
['{Key11: Value11,\nKey12: Value12,\nKey13: Value13}', '{Key21:
Value21,\nKey22: Value22,\nKey23: Value23}']
py>
py> # build mapping of names to values
py> names = {}
py> for i in range(1, 4):
.... for j in range(1, 4):
.... s = '%i%i' % (i, j)
.... names['Key%s' % s] = int(s)
.... names['Value%s' % s] = 'V%s' % s
....
py>
py> # eval strings to get dicts
py> first_dict, last_dict = [eval(s, names) for s in dict_strs]
py> first_dict
{11: 'V11', 12: 'V12', 13: 'V13'}
py> last_dict
{21: 'V21', 22: 'V22', 23: 'V23'}
 
D

Daniel Bickett

Cappy2112 said:
dictionaries can NOT contain dictionaries.

Who told you this?
In my python, they can.
[snip]

You took his reply out of context. Fuzzyman asked him if *his*
dictionaries were to contain dictionaries, and the reply was no, they
will not.
 
C

Claudio Grondi

{Key11: Value11
....
{Keyn1: Valuen1
Keyn2: Valuen2
...
Keynn:Valuenn}

Each pair in a dictionary is separated by CRLF and in each dictionary
numbers of pairs can be different.
I need to read only the last dictionary.What is a
best solution?
Thanks
Lad

What about (not tested):

stmFile = file(r"Path\FileNameOfTheFileWithDictionaries")
strFile = stmFile.read()
strLastDict = strFile[strFile.rfind('\n{"):]

?

Claudio
 
F

Fuzzyman

By the way - all suggestions so far rely on chopping the last
dictionary out as a string (naturally) and then using 'eval' to
evaluate it.

This has the inherent security problem that embedded python code will
also be run. This is not just a security risk (which may *not* be an
issue) but might cause extremely unpredictable results - which will be
I guess. Text might be evaluated as an expression.

There already exists a small python module called ConstructParser by
John Berninger that will read dictionaries from strings and reconstruct
them without using eval. You'll still need to isolate the actual
dictionary you want.

Google for it, if you can't find it - email me and I'll send you a
copy. It's nice and small.....

If you want a script that will load and save dictionaries you could use
ConfigObj. It uses the text format of 'ini' files ( keyword=value), but
is very easy to use. http://www.voidspace.org.uk/python/configobj.html

Regards,


Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
L

Larry Bates

The data contains only references to variables in the
local namespace an not literal values. Since local
variable names cannot include '{' or '}' characters,
my solution does in fact meet the criteria outlined.

Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,729
Latest member
taulaju99

Latest Threads

Top