我正在使用 Sikuli(请参阅 sikuli.org),它使用 jython2.5.2。
以下是 Java 级别上 Region 类的摘要:
public class Region {
// < other class methods >
public int type(String text) {
System.out.println("javadebug: "+text); // debug output
// do actual typing
}
}
在Python级别有一个包装类:
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 对象时似乎转换错误。
我想知道这里到底出了什么问题以及如何解决这个问题,以便 pythonmethod 中输入的字符与 javamethod 中输入的字符相同。 感谢您的帮助
最佳答案
从 Jython 代码来看,您必须告诉 Java,该字符串是 UTF-8 编码的:
def type(self, text):
jtext = java.lang.String(text, "utf-8")
print "pythondebug: " + text // debug output
JRegion.type(self, jtext)
关于encoding - Jython 的 Java 和 Python 级别的不同编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13165510/