例如:

# -*- 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。

参考:http://www.cnblogs.com/Key-Ky/p/3608632.html

Python 中文Key 报错问题的更多相关文章

  1. Python中文注释报错的解决方法

    在Python的程序中加了中文注释会报错 解决方法是:在程序的最开始位置加入 # -- coding: utf-8 --

  2. Python 中文注释报错解决方法

    代码中一旦有了中文注释便会报错. 原因 如果文件里有非ASCII字符,需要在第一行或第二行指定编码声明. 解决方法 在第一行或是第二行加入这么一句# -- coding: utf-8 -- 完美解决

  3. python中文注释报错问题

    错误信息: SyntaxError: Non-ASCII character '\xe4' in file... 解决办法: 在文件第一行或第二行添加:# -*- coding: utf-8 -*- ...

  4. python中文注释报错

    # -*- coding: utf-8 -*-#coding=utf-8 在开头加这个

  5. 【python】python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence

    python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte ...

  6. 【python】python安装tensorflow报错:python No matching distribution found for tensorflow==1.12.0

    python安装tensorflow报错:python No matching distribution found for tensorflow==1.12.0 python版本是3.7.2 要安装 ...

  7. webstorm中sass编译时目录或内容包含中文字符报错

    ruby版本:ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-mingw32] sass版本:Sass 3.4.22 (Selective Steve) ...

  8. mac下python环境pip报错[SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) 的解决方法

    1.mac下python环境pip报错: issuserdeMacBook-Pro:~ issuser$ pip install pyinstallerCollecting pyinstaller  ...

  9. 记一次 Hibernate 插入数据中文乱码报错解决

    错误描述 程序运行,向表中插入数据(包含中文)报错:\xE6\xB2\x88\xE9\x9B\xAA... 但是自己另外新建一个数据库手动插入数据中文正常,同样修改数据库,表的编码之后同样不行.而且 ...

随机推荐

  1. SQL例题合集

    1. 查询Student表中的所有记录的Sname.Ssex和Class列. Select sname,ssex,class from student; 2. 查询教师所有的单位即不重复的Depart ...

  2. Android自动登录与记住密码

    // 获取实例对象 sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE); rem_pw ...

  3. How to: Set up Openswan L2TP VPN Server on CentOS 6

    Have you ever wanted to set up your own VPN server? By following the steps below, you can set up you ...

  4. jquery check box

    if ($("#eulaLine").is(':checked')) { var mobile = $("#mobile").val(); if (mobile ...

  5. Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  6. bootstrap的select2校验及不影响原来的格式

    <style> .has-error .select2-choice, .has-error .select2-choices, .has-error.simple .select2-ch ...

  7. 在cmd命令行中弹出Windows对话框

    有时候用bat写一些小脚本最后会弹出对话框提示操作成功,可以用mshta.exe来实现,它是Windows系统的相关程序,用来执行.HTA文件,一般计算机上面都有这个程序,实现如下: mshta vb ...

  8. Java for LeetCode 140 Word Break II

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  9. Greedy:Yogurt factory(POJ 2393)

    酸奶工厂 题目大意:酸奶工厂每个星期都要制造酸奶,成本每单位x,然后每个星期要生产y,然后酸奶厂有个巨大的储存室,可以无限储存酸奶,而且酸奶的品质不会变坏,每天储存要每单位花费S,求最小的成本. 简直 ...

  10. codeforces 472C.Make It Nondeterministic 解题报告

    题目链接:http://codeforces.com/problemset/problem/472/C 题目意思:给出 n 个 people(从第1行往下数,编号依次为1,2,...,n),每 个 p ...