1
encode() 方法以 encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。
# 语法
str.encode(encoding='UTF-8',errors='strict')
# 参数
encoding -- 要使用的编码,如"UTF-8"。
errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。
# 返回值
该方法返回编码后的字符串。
2
decode() 方法以 encoding 指定的编码格式解码字符串。默认编码为字符串编码。
# 语法
str.decode(encoding='UTF-8',errors='strict')
# 参数
encoding -- 要使用的编码,如"UTF-8"。
errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。
# 返回值
该方法返回解码后的字符串。
#!/usr/bin/python
str = "this is string example....wow!!!";
str = str.encode('base64','strict');
print "Encoded String: " + str;
print "Decoded String: " + str.decode('base64','strict')
# 输出
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!
中文
http://www.cnblogs.com/long2015/p/4090824.html
http://www.cnblogs.com/skynet/archive/2011/05/03/2035105.html
英文 ascII, 中文 gb2312, 中文统一(包括其他语言)使用unicode,utf-8是unicode的一种实现方式.
str -> decode('the_coding_of_str') -> unicode
unicode -> encode('the_coding_you_want') -> str
str是字节串,由unicode经过编码(encode)后的字节组成的.
# 声明方式
s = '中文'
s = u'中文'.encode('utf-8')
>>> type('中文')
<type 'str'>
# 求长度(返回字节数)
>>> u'中文'.encode('utf-8')
'\xe4\xb8\xad\xe6\x96\x87'
>>> len(u'中文'.encode('utf-8'))
6
unicode才是真正意义上的字符串,由字符组成
# 声明方式
s = u'中文'
s = '中文'.decode('utf-8')
s = unicode('中文', 'utf-8')
>>> type(u'中文')
<type 'unicode'>
# 求长度(返回字符数)
>>> u'中文'
u'\u4e2d\u6587'
>>> len(u'中文')
2
搞明白要处理的是str还是unicode, 使用对的处理方法(str.decode/unicode.encode)
判断是否为unicode/str的方法
>>> isinstance(u'中文', unicode)
True
>>> isinstance('中文', unicode)
False
>>> isinstance('中文', str)
True
>>> isinstance(u'中文', str)
False
简单原则:不要对str使用encode,不要对unicode使用decode
>>> '中文'.encode('utf-8')
Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
>>> u'中文'.decode('utf-8')
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
不同编码转换,使用unicode作为中间编码
#s是code_A的str s.decode('code_A').encode('code_B')
文件处理,IDE和控制台
读文件 外部输入编码,decode转成unicode
处理(内部编码,统一unicode) encode转成需要的目标编码
写到目标输出(文件或控制台)
统一编码,防止由于某个环节产生的乱码(环境编码,IDE/文本编辑器, 文件编码,数据库数据表编码)
py文件默认编码是ASCII, 在源代码文件中,如果用到非ASCII字符,需要在文件头部进行编码
# -*- coding: utf-8 -*-
example
import urllib2, json
>>> resp = urllib2.urlopen('http://10.0.80.80/apis4machine/screenLog.php').read()
>>> resp
'{"status":0,"data":[{"channel":"ACM\\u5927\\u534e\\u5e9c","IP":"10.0.80.80","start_time":"2016-03-19 13:33:04","end_time":"2016-03-19 15:43:12","err_msg":"\\u8fde\\u7eed2\\u6b21\\u51fa\\u73b0\\u57ab\\u7247"}]}'
(中文是ACM大华府 : 连续2次出现垫片)
这个中文字符集是? \\u5927\\u534e\\u5e9c , 应该是unicode
unicode -> str,
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
2015-03-17 Centos 6/RHEL disable the IPv6 module.
2015-03-17 centreon公司推出的check plugin pack