Python与中文
来源:互联网 发布:快速排序算法java代码 编辑:程序博客网 时间:2024/06/05 19:55
作者:老王
前段时间受Erlang诱惑,疏远了Python。不过我已经迷途知返,今天聊聊Python与中文。
先看看Python3.0里面的情况:
首先,创建文件c:/chinese.py,文件编码是utf-8,文件内容如下:
print("中文")
在IDLE里执行:
>>> import sys
>>> sys.path.append("c://")
>>> import chinese
中文
一切都很完美!
再试试其它编码,把chinese.py的文件编码改成gbk,再执行上面的操作,结果报错:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import chinese
File "c:/chinese.py", line 1
SyntaxError: Non-UTF-8 code starting with '/xd6' in file c:/chinese.py on line 1,
but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
看来,要想一个办法声明编码才行,按照PEP0263的介绍修改chinese.py文件内容:
# coding=gbk
print("中文")
再执行上面的操作,一切顺利。
很多时候,你可能会发现其它形式的编码声明方式,如下:
# -*- coding: <encoding name> -*-
# vim: set fileencoding=<encoding name> :
这两种方式同样符合PEP0263的要求,并且在设定了python编码的同时还顺带设定了emacs / vim的编码。
再看看Python2.6里面的情况:
一样的chinese.py文件,如果不声明编码的话,不管是utf-8还是gbk,都会报错:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import chinese
File "c:/chinese.py", line 1
SyntaxError: Non-ASCII character '/xd6' in file c:/chinese.py on line 1,
but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (chinese.py, line 1)
总结:
使用非ASCII字符时,在Python3.0里面,如果是utf-8编码,可以不用声明编码。在Python2.X里面,不管什么编码都要声明编码!
========================================
Python资料:
http://wiki.woodpecker.org.cn/moin/
http://www.elias.cn/Develop/Python/
前段时间受Erlang诱惑,疏远了Python。不过我已经迷途知返,今天聊聊Python与中文。
先看看Python3.0里面的情况:
首先,创建文件c:/chinese.py,文件编码是utf-8,文件内容如下:
print("中文")
在IDLE里执行:
>>> import sys
>>> sys.path.append("c://")
>>> import chinese
中文
一切都很完美!
再试试其它编码,把chinese.py的文件编码改成gbk,再执行上面的操作,结果报错:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import chinese
File "c:/chinese.py", line 1
SyntaxError: Non-UTF-8 code starting with '/xd6' in file c:/chinese.py on line 1,
but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
看来,要想一个办法声明编码才行,按照PEP0263的介绍修改chinese.py文件内容:
# coding=gbk
print("中文")
再执行上面的操作,一切顺利。
很多时候,你可能会发现其它形式的编码声明方式,如下:
# -*- coding: <encoding name> -*-
# vim: set fileencoding=<encoding name> :
这两种方式同样符合PEP0263的要求,并且在设定了python编码的同时还顺带设定了emacs / vim的编码。
再看看Python2.6里面的情况:
一样的chinese.py文件,如果不声明编码的话,不管是utf-8还是gbk,都会报错:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import chinese
File "c:/chinese.py", line 1
SyntaxError: Non-ASCII character '/xd6' in file c:/chinese.py on line 1,
but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (chinese.py, line 1)
总结:
使用非ASCII字符时,在Python3.0里面,如果是utf-8编码,可以不用声明编码。在Python2.X里面,不管什么编码都要声明编码!
========================================
Python资料:
http://wiki.woodpecker.org.cn/moin/
http://www.elias.cn/Develop/Python/
- Python与中文
- Python Unicode与中文处理
- Python Unicode与中文处理
- Python Unicode与中文处理
- python与mysql遇到中文
- Python Unicode 与中文处理
- Python Unicode与中文处理
- Python Unicode与中文处理(文摘)
- python 中文编码处理与文件读写
- 详解python中文编码与处理
- Python 中文code与decode转换
- Python Unicode与中文处理(转)
- python与excel的中文编码问题
- Python中文处理:str与unicode
- 【转载】Python Unicode与中文处理
- Python 编码转换与中文处理
- Python正则匹配中文与编码总结
- Python 编码转换与中文处理
- java通过SOAP读取天气预报数据
- 学习HTTP协议的好书!
- 数据库视图对程序的影响
- js数组
- 用js获取当前页面中拥有焦点的对象
- Python与中文
- Windows 超过4G内存管理
- oracle中的游标
- csc实例:用UL制作横向CSS导航菜单
- 对表格的行和列都能加亮显示
- 解决vclskin可能存在的bug
- HTTP Digest Authentication在实际应用中的问题
- 什么是W3C标准
- 游客的旅游意外保险谁来买?