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


[EMAIL PROTECTED] wrote:
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

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to