簡體   English   中英

將unicode字符傳遞給Jython

[英]Passing unicode characters to Jython

我試圖在Jython上運行python代碼,這段代碼包含一些Unicode文字。 我想將代碼作為String傳遞(而不是從文件加載)。

似乎在exec()方法調用時,unicode字符被轉換為“?” 特點:

PythonInterpreter interp = new PythonInterpreter(null, new PySystemState());
System.out.println("ā".codePointAt(0)); // outputs 257
interp.exec("print ord(\"ā\")"); // outputs 63

我似乎無法找到一種方法如何將字符串傳遞給解釋器而不會弄亂這些字符。

我無法准確解釋發生了什么,但如果將unicode對象用作ord()參數並且Python代碼被編譯為PyCode對象,它對我有用:

import org.python.core.PyException;
import org.python.core.PyCode;
import org.python.util.PythonInterpreter;

public class Main {
  public static void main(String[] args) throws PyException {

    PythonInterpreter interp = new PythonInterpreter();
    System.out.println("ā".codePointAt(0));    // outputs 257
    interp.exec("print ord('ā')");             // outputs 63

    String s = "print ord(u'ā')";
    PyCode code = interp.compile(s);
    interp.exec(code);                         // outputs 257
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM