473,378 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

loading dictionary from a file

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
Feb 7 '08 #1
3 2902
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 <mi*********@gmail.com>
http://pythonwise.blogspot.com
Feb 7 '08 #2
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
Feb 7 '08 #3


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

key = value
"key2" = "value2"

Fuzzyman
http://www.voidspace.org.uk/python/weblog/index.shtml
Thanks
Feb 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Thomas Philips | last post by:
I want to print "1 spam 4 you" using a formatted string that gets its inputs from the dictionary d={'n1':1, 's1':'spam', 'n2':4}. To do so, I write >>> x="%('n1')d %('s1')s %('n2')d you" >>> x...
6
by: mercuryprey | last post by:
Hello, I want to do the following: def do_load(self, arg): sitefile = file('sitelist', 'r+', 1) while True: siteline = sitefile.readline() site_rawlist = siteline.split() sitelist] =...
12
by: Antoon Pardon | last post by:
Well at least I find them missing. For the moment I frequently come across the following cases. 1) Two files, each with key-value pairs for the same dictionary. However it is an error if the...
7
by: rickle | last post by:
I'm trying to compare sun patch levels on a server to those of what sun is recommending. For those that aren't familiar with sun patch numbering here is a quick run down. A patch number shows...
12
by: teoryn | last post by:
I've been spending today learning python and as an exercise I've ported a program I wrote in java that unscrambles a word. Before describing the problem, here's the code: *--beginning of file--*...
0
by: John Allman | last post by:
Hi all, I'm trying to create a setup which allows a program to request an object using strings and get an object of that type. It appears to be mostly working but i have difficulties if i...
9
by: oz | last post by:
Hi All, I want to make a dictionary with windows application. My data is html format. therefore, i use webBrowser control on my windows form. If the user click any word in webBrowser control,...
6
by: kaens | last post by:
Hey everyone, this may be a stupid question, but I noticed the following and as I'm pretty new to using xml and python, I was wondering if I could get an explanation. Let's say I write a simple...
20
by: Nickolai Leschov | last post by:
Hello all, I am programming an embedded controller that has a 'C' library for using its system functions (I/O, timers, all the specific devices). The supplied library has .LIB and .H files. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.