Python编程语言在Java中的使用效果


Python编程语言在使用的时候需要和很多种编程语言进行融合。Java就是一个和Python编程语言接触最多的一个编程语言。下面我们就来看一看在Java中如何进行Python编程语言的调用。

  1. python.java   
  2. import java.util.List;   
  3. import org.python.core.PyException;   
  4. import org.python.core.PyInteger;   
  5. import org.python.core.PyObject;   
  6. import org.python.util.PythonInterpreter;   
  7. public class python {   
  8. public static void main(String []args)throws PyException   
  9. {   
  10. PythonInterpreter interp =new PythonInterpreter();   
  11. System.out.println("Hello, brave new world";   
  12. interp.exec("import sys";   
  13. interp.exec("print sys";   
  14. interp.set("a", new PyInteger(42));   
  15. interp.exec("print 'test='+str(a)";   
  16. interp.exec("x = 2+2";   
  17. PyObject x = interp.get("x";   
  18. System.out.println("x: "+x);   
  19. System.out.println("Goodbye, cruel world";   
  20. interp.exec("import re";   
  21. interp.exec("t =re.compile('^(0{2})IFCSUM)MANIFEST):')";   
  22. interp.exec(" a = t.search('00:IFCSUM:MANIFEST:').groups()";   
  23. PyObject k = interp.get("a");   
  24. List list = ((List)k.__tojava__(List.class));   
  25. //System.out.println(k);   
  26. System.out.println(list);   
  27. }   
  28. }  

以上就是对Python编程语言在Java中的具体应用。

评论关闭