1.Python进阶笔记——jython
来源:互联网 发布:知乎周刊 epub 编辑:程序博客网 时间:2024/06/06 12:22
1.Python进阶笔记——jython
Jython是一种完整的语言,而不是一个Java翻译器或仅仅是一个Python编译器,它是一个Python语言在Java中的完全实现。Jython也有很多从CPython中继承的模块库。最有趣的事情是Jython不像CPython或其他任何高级语言,它提供了对其实现语言的一切存取。所以Jython不仅给你提供了Python的库,同时也提供了所有的Java类。这使其有一个巨大的资源库。
下载地址:
https://sourceforge.net/projects/jython/
然后解压后,将其中的jython.jar添加到工程目录.
执行Python语句
代码如下:
import org.python.util.PythonInterpreter;
publicclass TwoPanel
{
publicstaticvoid main(String args[])
{
PythonInterpreter interpreter =new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun');");
interpreter.exec("print days[1];");
}//main
}
执行输入Tue.
调用Python中函数
定义JAVA函数文件test.py如下:
def adder(a, b):
return a+ b
调用代码如下:
importjavax.script.*;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
importjava.io.*;
importstaticjava.lang.System.*;
publicclass TwoPanel
{
publicstaticvoid main(String args[])
{
PythonInterpreterinterpreter =new PythonInterpreter();
interpreter.execfile("F:\\java_zhizuo\\GUI-TEST\\test.py");
PyFunction func =(PyFunction)interpreter.get("adder",PyFunction.class);
inta = 2010,b = 2 ;
PyObject pyobj =func.__call__(new PyInteger(a),new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());
}//main
}
执行结果:
anwser = 2012
直接执行Python
创建python文件如下,test.py
print 'hello'
number=[3,5,2,0,6]
print number
number.sort()
print number
number.append(0)
print number
print number.count(0)
print number.index(5)
执行代码如下:
importjavax.script.*;
importorg.python.core.PyFunction;
importorg.python.core.PyInteger;
importorg.python.core.PyObject;
import org.python.util.PythonInterpreter;
importjava.io.*;
importstaticjava.lang.System.*;
publicclass TwoPanel
{
publicstaticvoid main(String args[])
{
PythonInterpreter interpreter =new PythonInterpreter();
interpreter.execfile("F:\\java_zhizuo\\GUI-TEST\\test.py");
}//main
}
直接结果:
hello
[3, 5,2, 0, 6]
[0, 2,3, 5, 6]
[0, 2,3, 5, 6, 0]
2
3
常见问题
安装PyDev
在Help->Install new Software..安装PyDev插件
PyDev - http://pydev.org/updates
选择PyDev for Eclipse选项
console: Failed to install '': java.nio.charset.UnsupportedCharsetException:cp0.
在用jython在eclipse中开发的时候,控制台的输出可能会出现
console: Failed to install ”: Java.nio.charset.UnsupportedCharsetException:cp0.
这样一个错误,此时如果控制台的错误堆栈中有中文的时候,中文就会显示为乱码,解决办法为在要执行的代码上右键,
Run As>Run Configurations,选择第二个页签Arguments,在VM arguments中添加
-Dpython.console.encoding=UTF-8
- 1.Python进阶笔记——jython
- Java调用python——Jython
- jython学习笔记(一)-----Java调用python
- Python与Java之间的相互调用——Jython
- Jython & Python
- Jython-Java操作Python脚本学习笔记(一)
- Jython-Java操作Python脚本学习笔记(二)
- python进阶学习笔记(大纲)——摘自慕课网 python进阶
- Split a string by spaces — preserving quoted substrings — in Python/Jython
- Jython & Python Examples
- python vs jython
- Jython 和 Python 对比
- Python Jython 记录 ImportError
- python 进阶4 笔记
- Python进阶学习笔记
- Python学习笔记(进阶)
- Python学习笔记(进阶)
- python进阶笔记
- 循环链表的经典问题
- 图-邻接矩阵
- USB HID设备报告描述符详解
- 29_Break rules
- Spring Boot:消费一个RESTful的网络服务(译)
- 1.Python进阶笔记——jython
- 单元测试方法的使用
- There is insufficient memory for the Java Runtime Environment to continue
- mysql 5.3.9 for mac 忘记密码
- gradle项目发布到自定义的tomcate中
- ubuntu16.04_tensorflow_gpu加速_源码安装笔记
- 2.笔记Erlang语言——基本操作
- C语言指针详细解答
- TensorFlow学习笔记(二):快速理解Tutorial第一个例子-MNIST机器学习入门 标签: 机器学习SoftmaxTensorFlow教程 2016-08-02 22:12 3729人阅