今天在群聊的时候,有同学发了最新版Google拼音的功能介绍,其中英文提示:打英文时只需输入前几个字母,输入法自动提示您可能要找的单字。
就是这个功能,我联想到了每天都要做的事情:编程。第一个浮现在脑海里的是VS里的智能感知,于是打开很久没打开的VS2008来回味。
然后我在想,输入法能做到这样吗?为了看看英文提示的效果,我下载了google拼音输入法。
输入我们最喜欢的hello world.
果然是有提示。然后我输入一个python的open方法
,在此刻,我想到的是如果
的正下方有open的api文档说明和使用示例该多好啊。就像VS2008的智能感知的截图一样。
让智能感知脱离IDE而存在,让我们这些程序员能在文本文档下编程也有智能感知的功能,只需要我们的输入法支持就可以了。
于是编程输入法的设想就这样出现,至于谁先去实现?我想谁都可以。
再看看Eclipse的智能感知:
同样的效果,不过条件都是需要装IDE。
相信不久的将来,我们肯定会用上这个输入法的。
昨天在保存一些中文字符到文本文档时,发现一个很奇怪的现象。先看看代码:
#coding=utf-8
import os
def write_use_open(filepath):
try:
file = open(filepath, 'wb')
try:
content = '中华人民共和国abcd \r\nee ?!>??@@@!!!!!???¥@#%@%#xx学校ada\r\n'
print file.encoding
print file.newlines
print file.mode
print file.closed
print content
file.write(content)
finally:
file.close()
print file.closed
except IOError, e:
print e
if __name__ == '__main__':
filepath = os.path.join(os.getcwd(), 'file.txt')
write_use_open(filepath)
开始我是IDLE编写的,并直接按F5运行,没发现问题,文件也被正确地保存,文件的编码类型也是utf-8.
可是我用命令行运行,却发现显示出现乱码了,然后在打开文件发现文件被正确保存了,编码还是utf-8:
我想问题是命令行不能自动识别字符编码吧,因为IDLE显示是正确的,它支持utf-8。
于是我修改了代码,在字符串前加了'u',表明content是unicode:
content = u'中华人民共和国abcd \r\nee ?!>??@@@!!!!!???¥@#%@%#xx学校ada\r\n'
可是运行发现,命令行是正确显示了,但是却出现异常:
很明显,content里包含了非ASCII码字符,肯定不能使用ASCII来进行编码的,write方法是默认使用ascii来编码保存的。
很容易就可以想到,在保存之前,先对unicode字符进行编码,我选择utf-8
#coding=utf-8
import os
def write_use_open(filepath):
try:
file = open(filepath, 'wb')
try:
content = u'中华人民共和国abcd \r\nee ?!>??@@@!!!!!???¥@#%@%#xx学校ada\r\n'
print file.encoding
print file.newlines
print file.mode
print file.closed
print content
print unicode.encode(content, 'utf-8')
file.write(unicode.encode(content, 'utf-8'))
finally:
file.close()
print file.closed
except IOError, e:
print e
if __name__ == '__main__':
filepath = os.path.join(os.getcwd(), 'file.txt')
write_use_open(filepath)
看看运行结果:
OK了打开文档也是正确的。
读取文件又怎样?同样道理,只是这次不是编码了,而解码:
def read_use_open(filepath):
try:
file = open(filepath, 'rb')
try:
content = file.read()
content_decode = unicode(content, 'utf-8')
print 'original text'
print content
print 'decode using utf-8'
print content_decode
finally:
file.close()
except IOError, e:
print e
if __name__ == '__main__':
filepath = os.path.join(os.getcwd(), 'file.txt')
write_use_open(filepath)
print 'read file ---------------------------'
read_use_open(filepath)
为什么不直接在open的时候就解码呢?呵呵,可以啊,可以使用codecs的open方法
import codecs
def read_use_codecs_open(filepath):
try:
file = codecs.open(filepath, 'rb', 'utf-8')
try:
print 'using codecs.open'
content = file.read()
print content
finally:
file.close()
except IOError, e:
print e
好了,希望对你有用。
本文参考:Unicode HOWTO