一、序列化数据
引入pickle包
dumps进行序列化,loads进行反序列化
1、序列化列表
# coding=utf-8 import pickle status_list = ["one","two","three"] pickle_data = pickle.dumps(status_list) new_status_list = pickle.loads(pickle_data) print new_status_list[1]
输出结果:two
2、序列化字典
# coding=utf-8 import pickle status_list = {"one":100,"two":200,"three":300} pickle_data = pickle.dumps(status_list) new_status_list = pickle.loads(pickle_data) print new_status_list[‘one‘]
输出的结果:100
二、json数据
引入json包,使用dumps和load进行encode和decode
1、列表数据转为json
import json status_list = ["one","two","three"] json_data = json.dumps(status_list) new_status_list = json.loads(json_data) print new_status_list[1]
输出结果:two
2、字典数据转为json
import json status_list = {"one":100,"two":200,"three":300} json_data = json.dumps(status_list) new_status_list = json.loads(json_data) print new_status_list[‘one‘]
输出结果:100
原文:http://www.cnblogs.com/kezheng/p/5161667.html