惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
GRAHAM CLULEY
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V2EX - 技术
V2EX - 技术
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
Webroot Blog
Webroot Blog
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
Hacker News: Ask HN
Hacker News: Ask HN
N
News and Events Feed by Topic
H
Heimdal Security Blog
H
Help Net Security
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
F
Full Disclosure
小众软件
小众软件
S
Securelist
罗磊的独立博客
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
S
Schneier on Security
D
DataBreaches.Net
M
MIT News - Artificial intelligence
V
Vulnerabilities – Threatpost
N
News and Events Feed by Topic
有赞技术团队
有赞技术团队
F
Fortinet All Blogs
T
Tenable Blog
The Register - Security
The Register - Security
C
Check Point Blog
AWS News Blog
AWS News Blog
Cloudbric
Cloudbric
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google Online Security Blog
Google Online Security Blog
博客园 - 叶小钗
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 司徒正美

博客园 - 观无明

IE9 + django开发版WEB服务器 不响应或Socket报错 google chrome 下django用户登录失败的问题 django 开发多语言网站 不愁男女比例不协调 Word文件如源码一样也可比较和合并 djang 测试心得 my django development environment (virtualenv+pip+django) nginx+fastcgi+django实践笔记 django 开发 - 小心模板文件的编码格式(utf-8) 汉王电子书D20使用笔记 使用south实现Django的数据库升级迁移 VIM 笔记 (for python ) 在Windows上使用Linux命令 cygwin 仍有人在真心关注这灾难 CruiseControl中应用NCover和NCoverExplore CruiseControl中使用NUnit中测试WEB服务 Resharper封装(Encapsulate)域Field为属性Property的命名问题 数据库开发的持续集成 - CruiseControl.Net的项目配置 数据库开发的持续集成 - Liquibase的简介和应用
python 的字符编码和中文处理 - 观无明 - 博客园
观无明 · 2010-05-14 · via 博客园 - 观无明

字符串

python有两种字符串

byteString = "hello world! (in my default locale)"

unicodeString = u"hello Unicode world!"

相互转换

1 s = "hello normal string"

2 u = unicode( s, "utf-8" )

3 backToBytes = u.encode( "utf-8" )

3 backToUtf8 = backToBytes.decode(‘utf-8’) #与第二行效果相同

如何判断

if isinstance( s, str ): # 对Unicode strings,这个判断结果为False

if isinstance( s, unicode): # 对Unicode strings,这个判断结果为True

if isinstance( s, basestring ): # 对两种字符串,返回都为True

做个试验

样例

看输出结果,注意下列事实:

python系统缺省的编码格式为ASCII,这个缺省编码在Python转换字符串时用的到,这里给两个例子:

1. a = "abc" + u"bcd", Python会如此转换"abc".decode(sys.getdefaultencoding()) 然后将两个Unicode字符合并。

2. print unicode('中文') , 这句话执行会出错“UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 …”,是因为Python试图用缺省编码来编码,而这个字符串不是ASCII,因此需要显示的指出,如果你的文件源类型为utf-8,则应如此:print unicode('中文','utf-8’)

Windows下getfilesystemencoding输出mbcs(多字节编码,windows的mbcs,也就是ansi,它会在不同语言的windows中使用不同的编码,在中文的windows中就是gb系列的编码)

Windows下控制台编码为cp936, 当你打印东西到控制台时Python自动做了转换。这里会引发一个有趣的问题, 试一下这个简单的例子test.py:

样例

在控制台中分别运行 python test.py 和 python test.py > 1.txt

你会发现后者会报错,原因是打印控制台时Python会自动转换编码到sys.stdout.encoding, 而输出到文件时Python不会自动在write调用中进行内部字符转换。这个问题在PrintFails中有较详细的说明。

UTF-8编码格式

保存utf-8格式的文件

import codecs

fileObj = codecs.open( "someFile", "r", "utf-8" )

u = fileObj.read() # Returns a Unicode string from the UTF-8 bytes in the file

自己写BOM头

out = file( "someFile", "w" )

out.write( codecs.BOM_UTF8 )

out.write( unicodeString.encode( "utf-8" ) )

out.close()

自己去掉BOM头

对UTF-16, Python将BOM解码为空字串。然而对UTF-8, BOM被解码为一个字符,如例:

样例

不知道为什么会这样不同,因此你需要在读文件时自己去掉BOM:

去掉BOM 

源码文件的编码

关于Python对代码文件的编码处理,PEP0263 讲的很清楚,现摘录如下

python缺省认为文件为ASCII编码。

可在代码头一行或二行加入声明文件编码申明,通知python该文件的编码格式,如

     # -*- coding: utf-8 –*-   # 注意使用的编辑器,确保文件保存时使用了该编码格式

  1. 对于Windows这样的平台,它使用了BOM(文件头三个字节 \xef\xbb\xbf)来申明文件为utf-8编码,这种情况下:
  • 如果文件中没有编码申明,python以utf8处理
  • 如果有编码申明但不是utf-8, python报错