[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

loading dictionary from a file

wildwest

2/7/2008 1:08:00 AM

Need a python trick, if it exists:

I have a file that stores key, value in following format
--
"v1" : "k1",
"v2" : "k2"
--

Is there a way to directly load this file as dictionary in python. I
could do (foreach line in file, split by ":" and then do dictionary
insert). Wondering, if some python built-in function can just read a
valid dictionary-file and load it?

Thanks
5 Answers

Miki

2/7/2008 1:18:00 AM

0

Hello Amit,

> Need a python trick, if it exists:
>
> I have a file that stores key, value in following format
> --
> "v1" : "k1",
> "v2" : "k2"
> --
>
> Is there a way to directly load this file as dictionary in python. I
> could do (foreach line in file, split by ":" and then do dictionary
> insert). Wondering, if some python built-in function can just read a
> valid dictionary-file and load it?
def load_as_dict(filename):
return eval("{" + open(filename).read() + "}")

Note that this is a very big security hole.

HTH,
--
Miki <miki.tebeka@gmail.com>
http://pythonwise.bl...

Ben Finney

2/7/2008 1:34:00 AM

0

Amit Gupta <emailamit@gmail.com> writes:

> Need a python trick, if it exists:
>
> I have a file that stores key, value in following format
> --
> "v1" : "k1",
> "v2" : "k2"
> --
>
> Is there a way to directly load this file as dictionary in python.

That input looks almost like valid JSON <URL:http://jso....

If you can easily massage it into JSON format, you can use the Python
JSON library <URL:http://cheeseshop.python.org/pypi/pytho...:

import json

input_text = open('foo.txt').read()
input_json = "{%(input_text)s}" % vars()

reader = json.JsonReader()
data = reader.read(input_json)

If the 'input_json' above actually is valid JSON, that will give the
corresponding Python data object.

This avoids the massive security hole of performing 'eval' on
arbitrary user input; the input isn't executed, merely parsed (as
JSON) to create a data object.

--
\ â??I busted a mirror and got seven years bad luck, but my lawyer |
`\ thinks he can get me five.â? â??Steven Wright |
_o__) |
Ben Finney

wildwest

2/7/2008 2:06:00 AM

0

On Feb 6, 5:33 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
> Amit Gupta <emaila...@gmail.com> writes:
> > Need a python trick, if it exists:
>
> > I have a file that stores key, value in following format
> > --
> > "v1" : "k1",
> > "v2" : "k2"
> > --
>
> > Is there a way to directly load this file as dictionary in python.
>
> That input looks almost like valid JSON <URL:http://jso....
>
> If you can easily massage it into JSON format, you can use the Python
> JSON library <URL:http://cheeseshop.python.org/pypi/pytho...:
>
> import json
>
> input_text = open('foo.txt').read()
> input_json = "{%(input_text)s}" % vars()
>
> reader = json.JsonReader()
> data = reader.read(input_json)
>
> If the 'input_json' above actually is valid JSON, that will give the
> corresponding Python data object.
>
> This avoids the massive security hole of performing 'eval' on
> arbitrary user input; the input isn't executed, merely parsed (as
> JSON) to create a data object.
>
> --
> \ "I busted a mirror and got seven years bad luck, but my lawyer |
> `\ thinks he can get me five." --Steven Wright |
> _o__) |
> Ben Finney

Great Inputs from both the posters.

Thanks

Adonis Vargas

2/7/2008 4:43:00 AM

0

Amit Gupta wrote:
> Need a python trick, if it exists:
>
> I have a file that stores key, value in following format
> --
> "v1" : "k1",
> "v2" : "k2"
> --
>
> Is there a way to directly load this file as dictionary in python. I
> could do (foreach line in file, split by ":" and then do dictionary
> insert). Wondering, if some python built-in function can just read a
> valid dictionary-file and load it?
>
> Thanks

If you could change the input data you can pickle the dict object then
save and reopen as needed.

import cPickle

data = {'k1': 'v1', 'k2': 'v2'}
output = open('output.dat', 'wb')
cPickle.dump(data, output)
output.close()
data1 = cPickle.load(open('output.dat', 'rb'))
print type(data1)
print data1

Hope this helps.

Adonis Vargas

Fuzzyman

2/7/2008 8:55:00 AM

0



Amit Gupta wrote:
> Need a python trick, if it exists:
>
> I have a file that stores key, value in following format
> --
> "v1" : "k1",
> "v2" : "k2"
> --
>
> Is there a way to directly load this file as dictionary in python. I
> could do (foreach line in file, split by ":" and then do dictionary
> insert). Wondering, if some python built-in function can just read a
> valid dictionary-file and load it?
>

If you change the format slightly you could use ConfgiObj <
http://www.voidspace.org.uk/python/conf... >.

key = value
"key2" = "value2"

Fuzzyman
http://www.voidspace.org.uk/python/weblog/i...



> Thanks