loading dictionary from a file

A

Amit Gupta

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
 
M

Miki

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,
 
B

Ben Finney

Amit Gupta said:
Need a python trick, if it exists:

I have a file that stores key, value in following format

That input looks almost like valid JSON <URL:http://json.org/>.

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

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.
 
A

Amit Gupta

That input looks almost like valid JSON <URL:http://json.org/>.

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

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
 
A

Adonis Vargas

Amit said:
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
 
F

Fuzzyman

Amit said:
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/configobj.html >.

key = value
"key2" = "value2"

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

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top