歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Python2不同版本安裝json模塊

Python2不同版本安裝json模塊

日期:2017/2/28 13:47:30   编辑:Linux教程

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 'dict'>
>>> type(b)
<type 'str'>
>>>

3、安裝腳本 install_simplejson.sh

#!/bin/bash

cat >/usr/local/src/test.py <<EOF
#!/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://[email protected]/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

Copyright © Linux教程網 All Rights Reserved