最近幾天老大要我寫一個python的爬蟲,鉆研了幾天終于小有所成,正當我準備大成的時候,下載了jython用不了,一直報錯(下面的代碼,復制網上找來的demo),我用的是mac,IDE用的裝了prdev插件的eclipse,python3.6,jython2.7,現在有幾個疑問,希望論壇里懂的大神救救我,感謝!
1 版本不兼容的問題
2 mac的jypthon沒安裝好,在終端輸入jython,沒反應
3 eclispse是不是少設定了什么,尤其是在編譯器上
Properties props = new Properties();
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import sys");
interpreter.exec("sys.path.append('/Users/zhong/jython2.7.0/Lib')");//jython自己的
interpreter.exec("sys.path.append('/Users/zhong/jython2.7.0/Lib/site-packages')");//jython 加載腳本的Python的jar包
interpreter.execfile("./pythonProfile/getPython1.py");
PyFunction func = (PyFunction)interpreter.get("adder",PyFunction.class);
int a = 10, b = 2 ;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());
Exception in thread "main" java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
at org.python.core.PyString.<init>(PyString.java:64)
at org.python.core.PyString.<init>(PyString.java:70)
at org.python.core.PySyntaxError.<init>(PySyntaxError.java:33)
at org.python.core.ParserFacade.fixParseError(ParserFacade.java:95)
at org.python.core.ParserFacade.parse(ParserFacade.java:190)
at org.python.core.Py.compile_flags(Py.java:1956)
at org.python.core.__builtin__.execfile_flags(__builtin__.java:527)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:286)
at test1.Python_Java.main(Python_Java.java:46)
看著網上的呼叫都是成功的,我這里就是用不了。
uj5u.com熱心網友回復:
java里面字串是unicode,python要位元組字串uj5u.com熱心網友回復:
那我是要把JAVA的字串變成位元組轉過去嗎?可以說得詳細點嗎?謝謝
uj5u.com熱心網友回復:
是的。需要轉換一下。
uj5u.com熱心網友回復:
先導好包 jython.jar用 new pyString(字串);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/34323.html
標籤:Java相關
上一篇:要給一個物件中的每個子物件的某個屬性都賦值,除了回圈還有別的方法嗎
下一篇:Java
