Home  >  Article  >  Backend Development  >  Python字符串的encode与decode研究心得乱码问题解决方法

Python字符串的encode与decode研究心得乱码问题解决方法

WBOY
WBOYOriginal
2016-06-06 11:27:001214browse

为什么会报错“UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)”?本文就来研究一下这个问题。


字符串在Python内部的表示是 代码如下:


#!/usr/bin/env python
#coding=utf-8
s="中文"

if isinstance(s, unicode):
#s=u"中文"
print s.encode('gb2312')
else:
#s="中文"
print s.decode('utf-8').encode('gb2312')

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Python 学习笔记Next article:python 文件与目录操作