Python中文
首页
教程
问答
标签
搜索
登录
注册
为什么python json.dumps抱怨ascii解码?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我的代码中有以下几行</p> <pre><code>outs = codecs.getwriter('utf-8')(sys.stdout) # dJSON contains JSON message with non-ASCII chars outs.write(json.dumps(dJSON,encoding='utf-8', ensure_ascii=False, indent=indent_val)) </code></pre> <p>我得到以下异常:</p> <pre><code> outs.write(json.dumps(dJSON,encoding='utf-8', ensure_ascii=False, indent=indent_val)) File "/usr/lib/python2.7/json/__init__.py", line 238, in dumps **kw).encode(obj) File "/usr/lib/python2.7/json/encoder.py", line 204, in encode return ''.join(chunks) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 27: ordinal not in range(128) </code></pre> <p>通过在<code>json.dumps</code>语句中指定<code>encoding='utf-8'</code>,我避免了这类问题。为什么我还是会犯错?</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>根据前面的回答,您可以使用<code>utf8</code>与<code>utf-8</code>来解决这个问题,但它不包括“copy-paste-this”修复。</p> <p>这是复制粘贴修复;p</p> <p><code>your_unicode_result = json.dumps(your_dict, encoding="utf8", ensure_ascii=False)</code></p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
无法使用Django/mongoengine连接到MongoDB(身份验证失败)
4 回答
无法使用Django\u mssql\u后端迁移到外部hos
10 回答
无法使用Django&Python3.4连接到MySql
1 回答
无法使用Django+nginx上载媒体文件
1 回答
无法使用Django1.6导入名称模式
5 回答
无法使用Django1.7和mongodb登录管理站点
1 回答
无法使用Djangoadmin创建项目,进程使用了错误的路径,因为我事先安装了错误的Python
4 回答
无法使用Djangockedi验证CBV中的字段
8 回答
无法使用Djangocketditor上载图像(错误400)
8 回答
无法使用Djangocron进行函数调用
4 回答
无法使用Djangofiler djang上载文件
1 回答
无法使用Djangokronos
8 回答
无法使用Djangomssql provid
10 回答
无法使用Djangomssql连接到带有Django 1.11的MS SQL Server 2016
7 回答
无法使用Djangomssq迁移Django数据库
10 回答
无法使用Djangonox创建用户
2 回答
无法使用Djangopyodb从Django查询SQL Server
3 回答
无法使用Djangopython3ldap连接到ldap
10 回答
无法使用Djangoredis连接到redis
2 回答
无法使用Django中的FK创建新表
9 回答