- 积分
- 56836
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
通过和Jython开发团队的人沟通,对Jython对unicode编码支持的过程有了一定的了解,原先以为高版本Jython不支持中文的看法是错误的。从Jython 2.5以后对字符串以byte数组方式处理,和Jython 2.2有很大的区别,这是为了和CPython保持一致。Jeff Allen在email中的原文这样写到:
Up to (I think) v2.2, Jython proudly supported Unicode in its str type, in which the elements were 16-bit characters. After that, the Python language introduced a proper unicode type and from 2.5 we had to treat str as bytes. It follows that the object printed in your example would have to be a unicode literal u"xy", to have any chance of working. This is the same as in CPython.
通过他的提示,以及查阅Jython的源代码,琢磨出了在Jython高版本中(2.5和2.7)正确使用中文的方法。
1. 由于我是在Java程序中嵌入Jython脚本,用到Jython的PythonInterpreter和InteractiveConsole类(继承自PythonInterpreter),需要将PythonInterpreter的cflags.source_is_utf8设置为true,该参数缺省为false,会将字符串以“ISO-8859-1”编码转为byte数组,此编码是不支持中文的。
2. 还需要将Jython的SystemState缺省编码设为“utf-8”: Py.getSystemState().setdefaultencoding("utf-8"); 。
3. 在脚本第一行添加指示脚本文件所用编码的语句:
# coding=utf-8
注意等号两边不能有空格
4. 在用到中文字符的地方之前要加字母u,比如
a = u'中文'
一个简单的测试脚本:
通过上述修改,形成了MeteoInfo Java 1.1.7R1版本(最近版本有点多,哈哈),使用了Jython最新的版本2.7b3。
|
|