python json库安装_ujson 安装使用记录 UltraJSON——Python 的极速 JSON 编解码器 | 学步园...

博主在项目中寻找高效的Python JSON解析库,尝试了cjson,但遇到只支持字符串键的限制。接着,博主发现并安装了ujson,虽然编译过程出现警告,但最终成功导入并测试。ujson提供了类似json的方法,如`dumps`和`loads`,在将字典转换为JSON字符串时,注意所有键会被转换为字符串。
摘要由CSDN通过智能技术生成

由于项目需要使用python解析json数据,因此到网上找了一些python解析json模块方面的资料。

根据查找到的资料发现,人们使用的最多的json解析库是simplejson,json及cjson,而且按照网上的说法cjson的效率最好。

于是我就安装了cjson进行测试

>>> import cjson

>>> dir(cjson)

['DecodeError', 'EncodeError', 'Error', '__doc__', '__file__', '__name__', '__version__', 'decode', 'encode']

>>> w={1:2,3:4,'a':5}

>>> cjson.encode(w)

Traceback (most recent call last):

File "", line 1, in ?

cjson.EncodeError: JSON encodable dictionaries must have string/unicode keys

>>>

但是在测试过程中出现了错误,cjson模块只支持string/unicode的key,在网上找了一圈也没有找到解决办法。

随后在一篇博客中发现了ujson,号称是python中最好的json解析模块,详情请见博客:

ujson测试结果如下:

[root@portal-ol-213 python-cjson-1.0.6]# easy_install ujson

Searching for ujson

Reading http://pypi.python.org/simple/ujson/

Reading https://github.com/jskorpan/ultrajson

Reading http://www.esn.me

Reading https://github.com/esnme/ultrajson

Reading http://github.com/esnme/ultrajson

Best match: ujson 1.18

Downloading http://pypi.python.org/packages/source/u/ujson/ujson-1.18.zip#md5=8d033858770ff2222a001ee1fcd0ee15

Processing ujson-1.18.zip

Running ujson-1.18/setup.py -q bdist_egg --dist-dir /tmp/easy_install-6XioO2/ujson-1.18/egg-dist-tmp-NfNCU3

In file included from ./python/objToJSON.c:4:

./lib/ultrajson.h:298:7: warning: no newline at end of file

./python/objToJSON.c: In function ‘PyLongToINT64’:

./python/objToJSON.c:101: warning: unused variable ‘obj’

./python/objToJSON.c: In function ‘Dict_iterNext’:

./python/objToJSON.c:369: warning: passing argument 2 of ‘PyDict_Next’ from incompatible pointer type

./python/objToJSON.c: In function ‘objToJSON’:

./python/objToJSON.c:695: warning: initialization from incompatible pointer type

./python/objToJSON.c:706: warning: initialization from incompatible pointer type

./python/objToJSON.c: At top level:

./python/objToJSON.c:86: warning: ‘PyIntToINT32’ defined but not used

In file included from ./python/JSONtoObj.c:2:

./lib/ultrajson.h:298:7: warning: no newline at end of file

In file included from ./lib/ultrajsonenc.c:37:

./lib/ultrajson.h:298:7: warning: no newline at end of file

./lib/ultrajsonenc.c:121: warning: ‘fastcall’ attribute ignored

./lib/ultrajsonenc.c: In function ‘Buffer_EscapeStringUnvalidated’:

./lib/ultrajsonenc.c:199: warning: value computed is not used

./lib/ultrajsonenc.c: At top level:

./lib/ultrajsonenc.c:414: warning: ‘fastcall’ attribute ignored

./lib/ultrajsonenc.c:857:2: warning: no newline at end of file

./lib/ultrajsonenc.c: In function ‘Buffer_EscapeStringValidated’:

./lib/ultrajsonenc.c:216: warning: ‘ucs’ may be used uninitialized in this function

In file included from ./lib/ultrajsondec.c:37:

./lib/ultrajson.h:298:7: warning: no newline at end of file

./lib/ultrajsondec.c:55: warning: ‘fastcall’ attribute ignored

./lib/ultrajsondec.c:75: warning: ‘fastcall’ attribute ignored

./lib/ultrajsondec.c:283: warning: ‘fastcall’ attribute ignored

./lib/ultrajsondec.c:301: warning: ‘fastcall’ attribute ignored

./lib/ultrajsondec.c:323: warning: ‘fastcall’ attribute ignored

./lib/ultrajsondec.c:341: warning: ‘fastcall’ attribute ignored

./lib/ultrajsondec.c:391: warning: ‘fastcall’ attribute ignored

./lib/ultrajsondec.c: In function ‘decode_string’:

./lib/ultrajsondec.c:424: warning: pointer targets in assignment differ in signedness

./lib/ultrajsondec.c: At top level:

./lib/ultrajsondec.c:620: warning: ‘fastcall’ attribute ignored

./lib/ultrajsondec.c: In function ‘decode_array’:

./lib/ultrajsondec.c:633: warning: value computed is not used

./lib/ultrajsondec.c: At top level:

./lib/ultrajsondec.c:670: warning: ‘fastcall’ attribute ignored

./lib/ultrajsondec.c:746: warning: ‘fastcall’ attribute ignored

zip_safe flag not set; analyzing archive contents...

Adding ujson 1.18 to easy-install.pth file

Installed /usr/lib/python2.4/site-packages/ujson-1.18-py2.4-linux-x86_64.egg

Processing dependencies for ujson

Finished processing dependencies for ujson

[root@portal-ol-213 python-cjson-1.0.6]#

[root@portal-ol-213 python-cjson-1.0.6]#

[root@portal-ol-213 python-cjson-1.0.6]# python

Python 2.4.3 (#1, Sep 21 2011, 19:55:41)

[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import ujson

>>> dir(ujson)

['__builtins__', '__doc__', '__file__', '__name__', '__version__', 'decode', 'dump', 'dumps', 'encode', 'load', 'loads']

>>> ujson.dump(1)

Traceback (most recent call last):

File "", line 1, in ?

TypeError: function takes exactly 2 arguments (1 given)

>>> ujson.dumps(1)

'1'

>>> ujson.dumps({1:2})

'{"1":2}'

>>>

>>> ujson.dumps({1:2,2:'a'})

'{"1":2,"2":"a"}'

>>> w=ujson.dumps({1:2,2:'a'})

>>> s=ujson.loads(w)

>>> s

{u'1': 2, u'2': u'a'}

>>> s.keys()

[u'1', u'2']

>>> s[1]

Traceback (most recent call last):

File "", line 1, in ?

KeyError: 1

>>> s['1']

2

>>>

使用json时需要注意的地方:python中字典的key在经过json转化后都变成了string类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值