1. 程式人生 > >python json模塊

python json模塊

ont author ast pac 理解 數據 finished ack python2


#!/usr/bin/env python

# -*- coding:utf-8 -*-

# @Time : 2017/11/14 14:24

# @Author : lijunjiang

# @File : Json.py

‘‘‘python json模塊 ‘‘‘

import codecs

import json

"""

JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。易於人閱讀和編寫。同時也易於機器解析和生成。

Pyhton的Json模塊提供了把內存中的對象序列化的方法

"""

# json.dumps 和 json.dump

# dump的功能是把python 對象 encode 為json對象的一個編碼過程

# json 模塊提供了json.dumps 和 json.dump 方法,區別是dump 直接到文件,dumps 到一個字符串,s可理解為string

# json.dumps :把python對象編碼為string

data = [{‘a‘:‘A‘, ‘b‘:(2, 4), ‘c‘:3.0}]

print(‘DATA: {0}‘,format(data))

print(type(data))

data_string = json.dumps(data)

print(‘JSON: {0}‘.format(data_string))

print(type(data_string))

‘‘‘

C:\Python27\python.exe D:/Python/modules/Json.py

DATA: {0} [{‘a‘: ‘A‘, ‘c‘: 3.0, ‘b‘: (2, 4)}]

<type ‘list‘>

JSON: [{"a": "A", "c": 3.0, "b": [2, 4]}]

<type ‘str‘>

Process finished with exit code 0

‘‘‘

# json.dump :把python 對象寫入文件

# python 對象不能直接寫入到文件,需要將其序列化之後才可以

# 直接將python對象寫入到方件

data = [{‘a‘

:‘A‘, ‘b‘:(2, 4), ‘c‘:3.0}]

with codecs.open(‘Json.txt‘, ‘w‘) as f:

f.write(data)

‘‘‘

C:\Python27\python.exe D:/Python/modules/Json.py

Traceback (most recent call last):

File "D:/Python/modules/Json.py", line 49, in <module>

f.write(data)

TypeError: expected a string or other character buffer object

Process finished with exit code 1

‘‘‘

# 直接將phthon 對象寫入文件會報:TypeError: expected a string or other character buffer object 錯誤

# 通過json.dump 寫入文件

data = [{‘a‘:‘A‘, ‘b‘:(2, 4), ‘c‘:3.0}]

with codecs.open(‘Json.txt‘, ‘w‘) as f:

json.dump(data, f)

with codecs.open(‘Json.txt‘, ‘r‘) as f:

json_txt = f.read()

print(json_txt)

‘‘‘

C:\Python27\python.exe D:/Python/modules/Json.py

[{"a": "A", "c": 3.0, "b": [2, 4]}]

Process finished with exit code 0

‘‘‘

# json.load 和 json.loads

# 作用是將 json對象decode解碼成python 可以識別的對象

# 與dump和dumps對應,json.load方法是基於文件的,json.loads 是基於字符串的

# json.loads : 將json 字符串 decode 為 Python 對象

data_str1 = ‘[{"a": "A", "c": 3.0, "b": [2, 4]}]‘

data_str2 = ‘{"a": "A", "c": 3.0, "b": [2, 4]}‘

print(‘data_str1: {0}‘.format(data_str1))

print(type(data_str1))

print(‘data_str2: {0}‘.format(data_str2))

print(type(data_str2))

data_py1 = json.loads(data_str1)

data_py2 = json.loads(data_str2)

print(‘data_py1: {0}‘.format(data_py1))

print(type(data_py1))

print(‘data_py2: {0}‘.format(data_py2))

print(type(data_py2))

‘‘‘

C:\Python27\python.exe D:/Python/modules/Json.py

data_str1: [{"a": "A", "c": 3.0, "b": [2, 4]}]

<type ‘str‘>

data_str2: {"a": "A", "c": 3.0, "b": [2, 4]}

<type ‘str‘>

data_py1: [{u‘a‘: u‘A‘, u‘c‘: 3.0, u‘b‘: [2, 4]}]

<type ‘list‘>

data_py2: {u‘a‘: u‘A‘, u‘c‘: 3.0, u‘b‘: [2, 4]}

<type ‘dict‘>

Process finished with exit code 0

‘‘‘

# 上例中,將字符串 data_str1 decode 為python 的list ,將data_str2 decode 為python的 dict 對象

# json.load : 將json 方件 decode 解碼為 python 對象

with codecs.open(‘Json.txt‘,‘r‘) as f:

print(‘f type: {}‘.format(type(f)))

Json_txt = f.read()

print(‘Json.txt info : {}‘.format(Json_txt))

with codecs.open(‘Json.txt‘, ‘r‘) as f:

json_txt_py = json.load(f)

print(‘json_txt_py: {}‘.format(json_txt_py))

print(type(json_txt_py))

‘‘‘

C:\Python27\python.exe D:/Python/modules/Json.py

f type: <type ‘file‘>

Json.txt info : [{"a": "A", "c": 3.0, "b": [2, 4]}]

json_txt_py: [{u‘a‘: u‘A‘, u‘c‘: 3.0, u‘b‘: [2, 4]}]

<type ‘list‘>

Process finished with exit code 0

‘‘‘

轉載自:https://www.cnblogs.com/lijunjiang2015/p/7833167.html

python json模塊