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

发表于:2010-5-10 10:27

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

#
java
#
Python

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

python.java

import java.util.List;

import org.python.core.PyException;

import org.python.core.PyInteger;

import org.python.core.PyObject;

import org.python.util.PythonInterpreter;

public class python {

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

  {

    PythonInterpreter interp =new PythonInterpreter();

    System.out.println("Hello, brave new world";

    interp.exec("import sys";

    interp.exec("print sys";

    interp.set("a", new PyInteger(42));

    interp.exec("print 'test='+str(a)";

    interp.exec("x = 2+2";

    PyObject x = interp.get("x";

    System.out.println("x: "+x);

    System.out.println("Goodbye, cruel world";

    interp.exec("import re";

    interp.exec("t =re.compile('^(0{2})IFCSUM)MANIFEST):')";

    interp.exec(" a = t.search('00:IFCSUM:MANIFEST:').groups()";

    PyObject k = interp.get("a");

    List list = ((List)k.__tojava__(List.class));

    //System.out.println(k);

    System.out.println(list);

  }

}

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

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号