|
|
|
|
|
|
| Author |
Message |
Fuzzyman *nix forums Guru
Joined: 21 Feb 2005
Posts: 406
|
Posted: Wed Feb 02, 2005 12:29 pm Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
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 |
|
| Back to top |
|
 |
Bruno Desthuilliers *nix forums Guru
Joined: 03 Mar 2005
Posts: 360
|
Posted: Wed Feb 02, 2005 12:38 pm Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
python@hope.cz a écrit :
| Quote: | 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'. |
|
| Back to top |
|
 |
Lad *nix forums addict
Joined: 23 Apr 2005
Posts: 50
|
Posted: Wed Feb 02, 2005 2:12 pm Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
Fuzzyman wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
Fuzzyman *nix forums Guru
Joined: 21 Feb 2005
Posts: 406
|
Posted: Wed Feb 02, 2005 2:42 pm Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
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)
| Quote: | 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 |
|
| Back to top |
|
 |
Larry Bates *nix forums Guru
Joined: 21 Feb 2005
Posts: 422
|
Posted: Wed Feb 02, 2005 3:08 pm Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
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
python@hope.cz wrote:
| Quote: | 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
|
|
|
| Back to top |
|
 |
Fuzzyman *nix forums Guru
Joined: 21 Feb 2005
Posts: 406
|
|
| Back to top |
|
 |
Miki Tebeka *nix forums addict
Joined: 22 Feb 2005
Posts: 61
|
Posted: Wed Feb 02, 2005 3:30 pm Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
Hello Lad,
| Quote: | 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.
--
------------------------------------------------------------------------
Miki Tebeka <miki.tebeka@zoran.com>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys |
|
| Back to top |
|
 |
Jeremy Bowers *nix forums Guru Wannabe
Joined: 23 Feb 2005
Posts: 129
|
Posted: Wed Feb 02, 2005 3:59 pm Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
On Wed, 02 Feb 2005 02:35:03 -0800, python wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
Steven Bethard *nix forums Guru
Joined: 20 Feb 2005
Posts: 773
|
Posted: Wed Feb 02, 2005 7:09 pm Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
python@hope.cz wrote:
| Quote: | 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'} |
|
| Back to top |
|
 |
Cappy2112 *nix forums beginner
Joined: 15 Apr 2005
Posts: 27
|
Posted: Thu Feb 03, 2005 12:45 am Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
| Quote: | dictionaries can NOT contain dictionaries.
|
Who told you this?
In my python, they can.
d1={1:"one"}
| Quote: | d2={2:"two"}
d1
{1: 'one'}
d2
{2: 'two'}
d3={3:d2}
d3
{3: {2: 'two'}} |
python@hope.cz wrote:
| Quote: | Fuzzyman wrote:
Can your dictionaries contain dictionaries ?
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. |
|
|
| Back to top |
|
 |
Daniel Bickett *nix forums beginner
Joined: 10 Apr 2005
Posts: 28
|
Posted: Thu Feb 03, 2005 1:40 am Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
Cappy2112 wrote:
| Quote: | 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.
--
Daniel Bickett
dbickett at gmail.com
http://heureusement.org/ |
|
| Back to top |
|
 |
Claudio Grondi *nix forums Guru Wannabe
Joined: 11 Apr 2005
Posts: 269
|
Posted: Thu Feb 03, 2005 10:39 am Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
| Quote: | {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 |
|
| Back to top |
|
 |
Fuzzyman *nix forums Guru
Joined: 21 Feb 2005
Posts: 406
|
|
| Back to top |
|
 |
Fuzzyman *nix forums Guru
Joined: 21 Feb 2005
Posts: 406
|
Posted: Thu Feb 03, 2005 12:47 pm Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
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 |
|
| Back to top |
|
 |
Larry Bates *nix forums Guru
Joined: 21 Feb 2005
Posts: 422
|
Posted: Thu Feb 03, 2005 4:01 pm Post subject:
Re: CONTEST - What is the (best) solution?
|
|
|
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
Fuzzyman wrote:
|
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Fri Jan 09, 2009 1:17 am | All times are GMT
|
|
Personal Finance | Car Loan | Internet Advertising | Free Ringtones | Credit Card Consolidation
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|