Python2不同版本安装json模块-演道网

Python2不同版本安装json模块

1、常用json库主要有json-py和simplejson

1) json-py 包含json和minjson,用法一样

Python 2.4.3 (#1, Jan  9 2013, 06:47:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import json
>>> import minjson
>>> a = {‘a’:5, ‘b’:4}
>>> b = json.write(a)
>>> c = json.read(b) 
>>> a
{‘a’: 5, ‘b’: 4}
>>> b
‘{“a”:5,”b”:4}’
>>> c
{‘a’: 5, ‘b’: 4}
>>> d = minjson.write(a)
>>> e = minjson.read(d)
>>> d
‘{“a”: 5, “b”: 4}’
>>> e
{‘a’: 5, ‘b’: 4}
>>>

2) simplejson

# python 2.6以上:
import json
json.loads(js_obj) 
json.dumps(py_obj)

# python 2.6以下:
import simplejson as json
json.loads(js_obj) 
json.dumps(py_obj)

2、安装simplejson

python2.6以上内置json解释库,是 smiplejson,python2.6以下没有内置的json,要手动安装。下载地址是  https://pypi.python.org/packages/source/s/simplejson/simplejson-2.0.9.tar.gz。如果下载太慢可使用pypi豆瓣源

[root@LuGu_10_1_80_198 py]# cd simplejson-2.0.9
[root@LuGu_10_1_80_198 simplejson-2.0.9]# /usr/bin/python2.4 setup.py install

[root@LuGu_10_1_80_198 simplejson-2.0.9]# /usr/bin/python2.4
Python 2.4.3 (#1, Jan  9 2013, 06:47:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import simplejson as json
>>> a = {‘a’: 5, ‘b’: 4}
>>> b = json.dumps(a)
>>> b
‘{“a”: 5, “b”: 4}’
>>> c = json.loads(b)
>>> c
{u’a’: 5, u’b’: 4}
>>> type(c)

>>> type(b)

>>>

3、安装脚本 install_simplejson.sh

#!/bin/bash

cat >/usr/local/src/test.py <#!/usr/bin/python
import sys

try:
    import json
except ImportError:
    try:
        import simplejson as json
    except ImportError:
        sys.exit(1)
sys.exit(0)
EOF

/usr/bin/python /usr/local/src/test.py &>/dev/null
if [ $? -eq 0 ];then
    echo “json or simplejson have installed”
    exit 0
fi

# json=”json-py-3_4.zip”  # python 2.6之前需要安装simplejson 2.6和之后内置json
simplejson=”simplejson-2.0.9.tar.gz”
setuptool=”setuptools-0.6c7-py2.4.egg”

rsync -arv rsync://root@10.1.17.217/upload/simplejson-forpy /usr/local/src &>/dev/null

cd /usr/local/src/simplejson-forpy

tar -xf ${simplejson} && softwaredir=`echo ${simplejson} | sed ‘s/\.tar.gz//g’`
mv ${setuptool} ${softwaredir} && cd ${softwaredir}
/usr/bin/python setup.py install >/dev/null 2>&1
cd /usr/local/src && rm -rf simplejson-forpy

/usr/bin/python /usr/local/src/test.py &>/dev/null
if [ $? -eq 0 ];then
    rm -rf /usr/local/src/test.py
    echo “Python simplejson Install OK” && exit 0
else
    rm -rf /usr/local/src/test.py
    echo “python simplejson  Install OK” && exit 1   
fi

零基础如何入门Python http://www.linuxidc.com/Linux/2016-10/136485.htm

Ubuntu 14.04安装Python 3.3.5  http://www.linuxidc.com/Linux/2014-05/101481.htm

CentOS上源码安装Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

在CentOS 6.5上安装Python2.7  http://www.linuxidc.com/Linux/2016-10/136206.htm

在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htm

转载自演道,想查看更及时的互联网产品技术热点文章请点击http://go2live.cn