Jython的Java和Python级别的不同编码

时间:2012-10-31 18:41:04

标签: encoding character-encoding jython sikuli jython-2.5

我正在使用使用jython2.5.2的Sikuli(参见sikuli.org)。

以下是Java级别上Region类的摘要:

public class Region {

    // < other class methods >

    public int type(String text) {
        System.out.println("javadebug: "+text); // debug output
        // do actual typing
    }
}

在Pythonlevel上有一个Wrapperclass:

import Region as JRegion            // import java class
class Region(JRegion):

    # < other class methods >

    def type(self, text):
        print "pythondebug: "+text  // debug output
        JRegion.type(self, text)

这适用于ascii字符,但当我使用ö,ä或ü作为文本时,会发生这种情况:

// python input:
# -*- encoding: utf-8 -*-
someregion = Region()
someregion.type("ä")

// output:
pythondebug: ä
javadebug: ä

传递给Java对象时,该字符似乎被错误地转换。

我想知道这里到底出了什么问题以及如何解决这个问题,以便在python方法中输入的字符在javamethod中是相同的。 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

从Jython代码看,您必须告诉Java,该字符串是UTF-8编码的:

def type(self, text):
    jtext = java.lang.String(text, "utf-8")
    print "pythondebug: " + text  // debug output
    JRegion.type(self, jtext)