例如:html
# -*- coding=UTF-8 -*- import json data = [] with open('data.json') as f: for line in f: data.append(json.loads(line)) strKey = '中評' strKey = strKey.decode('utf-8') # python 默認是unicode編碼,不轉碼,則查詢不到Key
print data[0].get(strKey)
字符串在Python內部的表示是unicode編碼,所以,在作編碼轉換時,一般須要以unicode做爲中間編碼,即先將其餘編碼的字符串解碼(decode)成unicode,再從unicode編碼(encode)成另外一種編碼。由於從文件讀取時,key和都被編碼爲utf-8,爲了查詢中文的Key,須要將默認的unicode編碼轉爲utf-8。python
參考:http://www.cnblogs.com/Key-Ky/p/3608632.htmljson