python中将string转化为dict的方法

我将数据库连接相关的一些用户名/密码/host/port等各种东西作为一个string保存在了数据库中,我要用MySQLdb检查这些数据库连接信息是够能正常使用,需要将数据库信息中的用户名/密码/host/port等信息作为参数传给MySQLdb.Connect()。者就需要将'{“host”:”192.168.11.22″, “port”:3306, “user”:”abc”,”passwd”:”123″, “db”:”mydb”, “connect_timeout”:10}’”这样的string转化为dict。

在python中,将string转为一个dict,我所知有如下3中方法:1. ast.literal_eval() 这是我常用的,依赖python2.6以上,据介绍时说比直接eval更安全一些,我没细究哈。2. eval() 在string内容比较可控/安全的前提下,eval是不错的方法。3. json.loads() 用json提供的loads方法是不错的,不过key/value中的string被转化为了unicode哦。

看实例代码:https://github.com/smilejay/python/blob/master/py2014/string2dict.py

'''Created on Oct 14, 2014@author: Jay <smile665@gmail.com>'''import MySQLdbimport astimport jsondef my_run():    try:        s = '{"host":"192.168.11.22", "port":3306, "user":"abc",\              "passwd":"123", "db":"mydb", "connect_timeout":10}'        d = ast.literal_eval(s)        print type(d)        print d        d1 = eval(s)        print type(d1)        print d1        d2 = json.loads(s)        print type(d2)        print d2        MySQLdb.Connect(host=d['host'], port=d['port'], user=d['user'],                        passwd=d['passwd'], db=d['db'],                        connect_timeout=d['connect_timeout'])        print 'right'    except Exception, e:        print 'wrong %s' % eif __name__ == '__main__':    my_run()

参考资料:

http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary

http://stackoverflow.com/questions/15596121/converting-a-string-into-dictionary-python

Original article: python中将string转化为dict的方法

©2015 笑遍世界. All Rights Reserved.

海内存知已,天涯若比邻。

python中将string转化为dict的方法

相关文章:

你感兴趣的文章:

标签云: