首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将波斯字符串( UTF-8 )从laravel API传递到python文件。

将波斯字符串( UTF-8 )从laravel API传递到python文件。
EN

Stack Overflow用户
提问于 2021-09-08 07:43:31
回答 1查看 79关注 0票数 1

在我的laravel-API中,我必须调用main.py文件并将波斯字符串UTF-8传递给main.py

这是laravel-API

代码语言:javascript
复制
$command = 'سلام چطوری؟'
$output = '';
exec("python ../../../python/main.py '$command'", $output);
dd($output);

这是main.py

代码语言:javascript
复制
import sys

def main(text):
    return text

x = sys.argv[1]
print(x)

问题是,当我传递这样的东西时,它正确地工作。

$command =“嗨,你好吗?”

代码语言:javascript
复制
//output
['hi how are you?']

但是当我像这样传递波斯线时,我没有得到任何回报。

$command =‘سلامچطوری؟’

代码语言:javascript
复制
#output
[]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-11 15:16:02

将此代码放在调用exec()shell_exec()之前

代码语言:javascript
复制
setlocale(LC_CTYPE, "en_US.UTF-8");
putenv("PYTHONIOENCODING=utf-8");

当您想将$command作为参数传递时,使用utf8_encode(),对于输出使用utf8_decode()

我已经用shell_exec()测试过这个,它对我来说很好。

摘要:

代码语言:javascript
复制
setlocale(LC_CTYPE, "en_US.UTF-8");
putenv("PYTHONIOENCODING=utf-8");
$command = utf8_encode($command);
$output = shell_exec("python path/to/your/.py/file '$command'");
$output = utf8_decode($output); // your final output
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69098912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档