当前位置: 首页 > 工具软件 > go-spring > 使用案例 >

go/beego框架或者Java-spring框架中调用python脚本

麹飞航
2023-12-01

go-python的集合
go语言的速度比python要快得多,但是go的生态没有python那么好,不过可以用go语言调用python脚本的方法来实现特定的功能。
例如:
python-展示

import numpy as np
#可视化--------------
def show_data():
    test_name = np.load('./static_pth_npy_pickle/names.npy', encoding="latin1")
    test_feature = np.load('./static_pth_npy_pickle/features.npy', encoding="latin1")
    print("存储书名", test_name)
    print("存储书籍编号", test_feature)
show_data()

python-删除

input_name = sys.argv[1]
#删除数据库里的数据
def python_delete(input_name):
        test_name = np.load('names.npy', encoding="latin1")
        test_feature = np.load('features.npy', encoding="latin1")
        test_name = test_name.tolist()
        test_feature = test_feature.tolist()
        for i in range(len(test_name)):
            try:
                f = test_name.index(input_name)
                print(f)
                while f != None:
                    test_name.remove(input_name)
                    del test_feature[f]
                    np.save('names.npy', test_name)
                    np.save('features.npy', test_feature)
                    # print('本地删除成功!')
            except:
                continue
        print('删除成功')

beego中的utils中加入工具方法


```go
//删除数据
func Delete_data(inputname string){
	cmd:=exec.Command("python","go_python/go_python.py",inputname)
	data,_:=cmd.Output()
	fmt.Println(string(data))
}

//显示数据
func Show_data(){
	cmd:=exec.Command("python","go_python/go_python.py")
	data,_:=cmd.Output()
	fmt.Println(string(data))
}

Java调用python脚本

public class python_api {

   static public void testxx() {
        String pyPath = "G:\\image_utils\\img\\api.py";
        String  float_num="&;0.10455145;";
        String[] args1 = new String[]{"python", pyPath,float_num };
        try {
            Process process = Runtime.getRuntime().exec(args1);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"GB2312"));
            String actionStr = in.readLine();
            if (actionStr != null) {
                System.out.println(actionStr);
            }
            in.close();
            process.waitFor();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    
    public static void main(String args[])
    {
        testxx();
    }
}```

 类似资料: