Size: 5046
Comment:
|
Size: 5108
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 16: | Line 16: |
== 相关模块 == * PythonicUnicode ~ Python 的Unicode 支持 |
== Unicode处置 == * PythonicUnicode ~ 收集Python 的Unicode应用解说 * [:PythonInUnicode:基于Unicode的Py 开发] |
Python 中文处理体验集 ::-- ZoomQuiet [DateTime(2007-11-07T03:11:54Z)] TableOfContents
1. 中文.编码
1.1. 背景知识
[:BPUG/2007-03-03:BPUG第15次.栖息谷文化传播现场会课] Unicode 应用交流( AlexDong )
1.2. Unicode处置
PythonicUnicode ~ 收集Python 的Unicode应用解说
[:PythonInUnicode:基于Unicode的Py 开发]
1.3. 体验集
{{{Oyster <[email protected]> reply-to [email protected], to "python-cn:CPyUG" <[email protected]>, date Nov 7, 2007 9:48 AM}}} subject [CPyUG:34634] [http://groups.google.com/group/python-cn/browse_thread/thread/4a2774a6e1f0b89a python程序的编码-大家帮忙看看对不对]
总结一下,看看对不对
- 源程序里面的汉字,直接用普通字符串的方式写出来 '汉字', 不要用unicode字符串的方式 u'汉字'
- 源程序保存为utf-8编码的文件,且文件头包含 #coding=utf-8 字眼。 其实,只要查找到了 "# coding 编码" 就行了,所以就算 乱写成encoding也无所谓。我个人喜欢
# coding="utf-8" 因为它象一个合法的python赋值语句
- 要输出一个字符串或者已赋值的字符串变量,需要
print unicode('汉字','utf-8') 糟糕的是,这里我们不能省略编码方式让其使用文件头指定的utf-8 因为我在py24上省略没有问题,但是py25就会报错
- 使用字符串作为函数调用的参数时,需要先转换为unicode对象
- unicode+str之后返回的就是unicode,所以print这样的值,不需要再次转换
print类的实例(或者对实例使用str函数)的时候,调用的是__str__函数, 返回的也是str类型的对象。这样的话,会报错的 unicode(对象),首先查找的是__unicode__函数,并将其值作为返回值;如果找不到, 则使用__str__函数
- 如果变量是raw_input进来的,则该变量是一个str类型的对象,需要使用
a)unicode(变量,sys.stdin.encoding)
b)name.decode(sys.stdin.encoding)
- 对于文件的io,应该使用codecs.open
测试用的很乱的代码
# coding='utf-8' import locale import sys def hi(n2, name): return '%s, %s' % (n2,name) name=raw_input('name>> ') n2='你好' print type(name), type(n2) print hi(n2,name) print hi(unicode(n2,'utf-8'),unicode(name,sys.stdin.encoding)) print hi(unicode(n2,'utf-8'),name.decode(sys.stdin.encoding)) print map(len, (unicode(n2,'utf-8'),name.decode(sys.stdin.encoding))) print map(type, (n2,name,unicode(n2,'utf-8'),unicode(name,sys.stdin.encoding))) print map(type,(hi(n2,name),hi(unicode(n2,'utf-8'),unicode(name, sys.stdin.encoding)))) print unicode(n2,'utf-8') + unicode(name,sys.stdin.encoding) print sys.stdin.encoding, sys.stdout.encoding print def info(name, age=0): return "I'm %s, and %0i years old" % (name, age) print '===1st test' print unicode(info(unicode('张','utf-8'), 20)) #这里的输出是对的 print 'length=', len(unicode('张','utf-8')) #长度计算也是对的 print print '===2nd test' name=raw_input('name>> ')#输入一个汉字,比如 李 print type(name) print name, unicode(name,sys.stdin.encoding), name.decode(sys.stdin.encoding) print map(len,(name, unicode(name,sys.stdin.encoding), name.decode(sys.stdin.encoding))) print unicode(name,sys.stdin.encoding)==name.decode(sys.stdin.encoding) print info(name, 21) #输出没有问题 print unicode(info(name, 21),sys.stdin.encoding) #输出没有问题 print info(unicode(name,sys.stdin.encoding), 21) #输出没有问题 print map(type, (info(name, 21), info(unicode(name,sys.stdin.encoding), 21))) print print '===3rd test' print type(name) #是string name2='李' print type(name) #还是string print name==name2 #False? print info(name, 21) #输出没有问题 print 'length=', len(name) #但是长度计算有问题 print class human(object): def __init__(self, name, age=0): self.name=name self.age=age def __str__(self): print 'in __str__' return "I'm %s, and %0i years old" %(self.name, self.age) def __unicode__(self): return "unistr: I'm %s, and %0i years old" % (self.name, self.age) print '===4th test' zhang=human(unicode('张','utf-8'),20) print 'zhang.name=',zhang.name #输出没有问题 print 'length=', len(zhang.name) #长度没有问题 print unicode(zhang) #输出没有问题 #print zhang #出错! #print str(zhang) #出错! #print unicode(str(zhang)) #出错! t=unicode(zhang) import codecs outfile = codecs.open("test.txt", "w", "utf-8") outfile.write(t) #outfile.write(t.decode('utf-8')) #outfile.write(unicode('汉字','utf-8')) outfile.close()