Java调用Python

  1. python.java  
  2.   
  3.   
  4. import java.util.List;  
  5.   
  6. import org.python.core.PyException;  
  7. import org.python.core.PyInteger;  
  8. import org.python.core.PyObject;  
  9. import org.python.util.PythonInterpreter;  
  10. public class python {  
  11.        public static void main(String []args)throws PyException  
  12.        {  
  13.                PythonInterpreter interp =new PythonInterpreter();  
  14.                
  15.                System.out.println("Hello, brave new world";  
  16.                
  17.                interp.exec("import sys";  
  18.                interp.exec("print sys";  
  19.                
  20.                interp.set("a"new PyInteger(42));  
  21.                interp.exec("print 'test='+str(a)";  
  22.                interp.exec("x = 2+2";  
  23.                PyObject x = interp.get("x";  
  24.                
  25.                System.out.println("x: "+x);  
  26.                System.out.println("Goodbye, cruel world";  
  27.                
  28.                interp.exec("import re";  
  29.   
  30.                interp.exec("t =re.compile('^(0{2})IFCSUM)MANIFEST):')";  
  31.                interp.exec(" a = t.search('00:IFCSUM:MANIFEST:').groups()";  
  32.                PyObject k = interp.get("a");  
  33.                List list = ((List)k.__tojava__(List.class));  
  34.   
  35.                //System.out.println(k);  
  36.                System.out.println(list);  
  37.   
  38.   
  39.        }  
  40. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值