go调⽤python脚本并输出数据1.go调⽤python脚本
#go调⽤python有编码问题,GbkToUtf8是⾃⼰封装的转换编码
command := "build.py"
cmd := exec.Command("python", command, name)
output, err := cmd.Output()
if err != nil {
fmt.Printf("Execute Shell:%s failed with error:%s", command, err.Error())
return
}
txt, _ := utils.GbkToUtf8(output)
fmt.Printf("Execute Shell:%s finished with output:\n%s", command, string(txt))
#go调⽤python直接报错status=-1,CombinedOutput 可以看到实际输出
cmd := exec.Command("python", command)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + string(output))
return
}
fmt.Println(string(output))
#GbkToUtf8 ⽅法
import (
"bytes"
"/x/text/encoding/simplifiedchinese"
"/x/text/transform"
"io/ioutil"
)
func GbkToUtf8(str []byte) (b []byte, err error) {
r := transform.NewReader(bytes.NewReader(str), simplifiedchinese.GBK.NewDecoder())
b, err = ioutil.ReadAll(r)
if err != nil {
return
}
return
}
python内部读取⽂件⽬录报错
from pathlib import Path
import os
path1 = os.path.abspath(os.path.dirname(__file__))
python怎么读取py文件with open(Path(path1) / settings.DATASET_PATH, 'r', encoding='utf-8') as f:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。