Skip to content

Commit bbc1ec4

Browse files
committed
Initial commit
Signed-off-by: sunus <[email protected]>
0 parents  commit bbc1ec4

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

1-pyutf8.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
# encoding=utf-8
3+
4+
import os
5+
import sys
6+
a = "我是中文"
7+
b = u"我也是中文"
8+
os.write(sys.stdin.fileno(), a)
9+
os.write(sys.stdin.fileno(), b)
10+
print a
11+
print b

pyutf8.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#Python, UTF-8, and Chinese characters.
2+
3+
######Python的UTF-8编码问题比较让人抓狂, 我之前抓狂过很多次之后决定写一个wiki,
4+
######主要是为了解决我们在Python编码过程中遇到的UTF-8中文乱码问题.
5+
6+
> 1. 关于文件的编码:
7+
8+
> > 文件的编码方式是什么?ASCII(default)还是其他的?在vim下可以用:set fileencoding查看.
9+
或者使用iconv: iconv -f utf8 filename 如果输出正常, 无错误, 无乱码, 那么该文件也应该是正确地以UTF-8的编码方式保存的.
10+
如果打算使用utf编码,可以使用:set fileencoding=utf8进行指定,vim会负责完成转换工作.
11+
12+
> > * 关于Python, PEP-0263: http://www.python.org/dev/peps/pep-0263/ 建议是在文件的头两行显示指定文件编码,方式有如下几种:
13+
14+
> > > 1. `# coding=<encoding name>`
15+
> > > 2. `# -*- coding: <encoding name> -*-`
16+
> > > 3. `# vim: set fileencoding=<encoding name>`
17+
18+
> > * 第3种比较少见,第2种比较漂亮,第1种是我使用的:)
19+
> > * 文件编码的作用:
20+
21+
> > > 1. 让阅读代码的人知道该文件的编码方式
22+
> > > 2. 如果文件中有__显式__的需要特别编码处理的字符(如中文)时,通过指定文件的编码能够让
23+
editor以及__python解析器__知道如何处理,储存.
24+
> > > 3. 显式具体指的是:
25+
26+
> > > > * a = "我是中文"
27+
> > > > * a = u"我是中文"
28+
29+
> > #!/usr/bin/env python
30+
# encoding=utf-8
31+
import os
32+
import sys
33+
a = "我是中文"
34+
b = u"我也是中文"
35+
os.write(sys.stdin.fileno(), a)
36+
os.write(sys.stdin.fileno(), b)
37+
print a
38+
print b
39+

0 commit comments

Comments
 (0)