java调用python脚本并传递参数list

简介: java调用python脚本并传递参数list

java传递1个list参数

@Test
public void oneList() {
    List<Double> list = new ArrayList<>();
    list.add(1.222);
    list.add(2.888);
    list.add(3.888);
    list.add(4.888);
    list.add(5.888);
    String flag = oneList(list);
    System.out.println(flag);
}
public String oneList(List<Double> list) {
    String result = null;
    try {
    // String python = "/usr/local/bin/python3.10"; // python
        String python = "python";  
        String file = "/app/hello_list01_arg.py";
        Process process = Runtime.getRuntime().exec(python + " " + file + " " + list);// 执行py文件
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result = line;
        }
        in.close();
        process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return result;
}
  • hello_list01_arg.py
import numpy as np
import sys
'''
接受java传递1个list参数
'''
def main():
    list_str = []
    for i in range(1, len(sys.argv)):
        list_str.append(sys.argv[i].replace(",", ""))
    list_str[0] = list_str[0].replace("[", "")
    list_str[len(sys.argv) - 2] = list_str[len(sys.argv) - 2].replace("]", "")
    # 字符串数组
    print(list_str)
if __name__ == '__main__':
    main()

java传递2个list参数

@Test
public void towList() {
List<Double> listOne = new ArrayList<>();
   listOne.add(1.222);
   listOne.add(2.888);
   listOne.add(3.888);
   listOne.add(4.888);
   listOne.add(5.888);
   List<Integer> listTwo = new ArrayList<>();
   listTwo.add(1);
   listTwo.add(2);
   listTwo.add(3);
   listTwo.add(4);
   listTwo.add(5);
   String flag = towList(listOne, listTwo);
   System.out.println(flag);
}
public String towList(List<Double> listOne, List<Integer> listTwo) {
    String result = null;
    try {
        // tring python = "/usr/local/bin/python3.10 "; // python
        String python = "python ";  
        String file = ResourcesUtil.getResource("py", "hello_list02_arg.py");
        result = ProcessUtils.exec(python + " " + file + " " + listOne + " " + listTwo);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
  • hello_list02_arg.py
import numpy as np
import sys
'''
接受java传递2个list参数
'''
def main():
  #初始数据
    # print (sys.argv[1:])
    list_str = []
    turn = []
    current = []
    for i in range(1, len(sys.argv)):
        list_str.append(sys.argv[i].replace(",", ""))
    list_str[0] = list_str[0].replace("[", "")
    list_str[len(sys.argv) - 2] = list_str[len(sys.argv) - 2].replace("]", "")
    str = ','.join(list_str)
    arr = str.split("][")
    turn = arr[0].split(",")
    turn = list(map(float, turn))#转换成浮点数数组
    #输出第一个数组
    print (turn)
    current = arr[1].split(",")
    current = list(map(int, current))#转换成整数数组
    #输出第二个数组
    print (current)
if __name__ == '__main__':
    main()


相关文章
|
25天前
|
安全 Linux 网络安全
利用Python脚本自动备份网络设备配置
通过本文的介绍,我们了解了如何利用Python脚本自动备份网络设备配置。该脚本使用 `paramiko`库通过SSH连接到设备,获取并保存配置文件。通过定时任务调度,可以实现定期自动备份,确保网络设备配置的安全和可用。希望这些内容能够帮助你在实际工作中实现网络设备的自动化备份。
51 14
|
1月前
|
Java
java代码优化:判断内聚到实体对象中和构造上下文对象传递参数
通过两个常见的java后端实例场景探讨代码优化,代码不是优化出来的,而是设计出来的,我们永远不可能有专门的时间去做代码优化,优化和设计在平时
33 15
|
2月前
|
Python
自动化微信朋友圈:Python脚本实现自动发布动态
本文介绍如何使用Python脚本自动化发布微信朋友圈动态,节省手动输入的时间。主要依赖`pyautogui`、`time`、`pyperclip`等库,通过模拟鼠标和键盘操作实现自动发布。代码涵盖打开微信、定位朋友圈、准备输入框、模拟打字等功能。虽然该方法能提高效率,但需注意可能违反微信使用条款,存在风险。定期更新脚本以适应微信界面变化也很重要。
213 61
|
2月前
|
C语言 Python
[oeasy]python054_python有哪些关键字_keyword_list_列表_reserved_words
本文介绍了Python的关键字列表及其使用规则。通过回顾`hello world`示例,解释了Python中的标识符命名规则,并探讨了关键字如`if`、`for`、`in`等不能作为变量名的原因。最后,通过`import keyword`和`print(keyword.kwlist)`展示了Python的所有关键字,并总结了关键字不能用作标识符的规则。
46 9
|
2月前
|
数据挖掘 vr&ar C++
让UE自动运行Python脚本:实现与实例解析
本文介绍如何配置Unreal Engine(UE)以自动运行Python脚本,提高开发效率。通过安装Python、配置UE环境及使用第三方插件,实现Python与UE的集成。结合蓝图和C++示例,展示自动化任务处理、关卡生成及数据分析等应用场景。
178 5
|
2月前
|
数据挖掘 大数据 数据处理
python--列表list切分(超详细)
通过这些思维导图和分析说明表,您可以更直观地理解Python列表切分的概念、用法和实际应用。希望本文能帮助您更高效地使用Python进行数据处理和分析。
78 14
|
2月前
|
数据挖掘 大数据 数据处理
python--列表list切分(超详细)
通过这些思维导图和分析说明表,您可以更直观地理解Python列表切分的概念、用法和实际应用。希望本文能帮助您更高效地使用Python进行数据处理和分析。
137 10
|
2月前
|
数据采集 存储 监控
21个Python脚本自动执行日常任务(2)
21个Python脚本自动执行日常任务(2)
129 7
21个Python脚本自动执行日常任务(2)
|
2月前
|
Android开发 开发者 Python
通过标签清理微信好友:Python自动化脚本解析
微信已成为日常生活中的重要社交工具,但随着使用时间增长,好友列表可能变得臃肿。本文介绍了一个基于 Python 的自动化脚本,利用 `uiautomator2` 库,通过模拟用户操作实现根据标签批量清理微信好友的功能。脚本包括环境准备、类定义、方法实现等部分,详细解析了如何通过标签筛选并删除好友,适合需要批量管理微信好友的用户。
108 7
|
3月前
|
监控 数据挖掘 数据安全/隐私保护
Python脚本:自动化下载视频的日志记录
Python脚本:自动化下载视频的日志记录

热门文章

最新文章

推荐镜像

更多