---
title: "解决UnicodeEncodeError: 'ascii' codec can't encode"
author: "vergilwang"
date: "2012-11-22T16:48:00Z"
tags: []
categories: ["编程语言"]
slug: "unicodeencodeerror-ascii-codec-can-t-encode"
draft: false
lastmod: "2014-02-15T15:51:18Z"
id: 2011307
url: "https://vergilwang.iteye.com/blog/2011307"
---

#coding = GBK

#coding = utf-8

解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3问题

转自：[http://cooler1217.iteye.com/blog/1465335](http://cooler1217.iteye.com/blog/1465335)

异常: 'ascii' codec can't encode characters

字符集的问题，在文件前加两句话：

reload(sys)  
sys.setdefaultencoding( "utf-8" )

完美解决，ok

领 当字符串里有 \\n、\\t、\\r时，json.loads()失效，异常，要去掉