为什么这个插件不解码$final?



这是我正在使用的插件:

add_shortcode( '3', 'execute_python_with_argv3' );
function execute_python_with_argv3() {
$description = array (     
array("pipe", "r"),  // stdin
array("pipe", "w"),  // stdout
);
$application_system = "python ";
$application_path .= plugin_dir_path( __FILE__ );
$application_name .= "3.py";
$separator = " ";
$application =     $application_system.$application_path.$application_name.$separator;
$pipes = array();
$proc = proc_open ($application, $description, $pipes);
if (is_resource ( $proc )) {
$response = stream_get_contents ($pipes [1] ); //Reading stdout buffer
}
$final = substr(str_replace("'", "", $response), 1);
echo $final;
echo iconv('UTF-8', 'UTF-8', $final);
}

$response等于b'xc4x8dxc4x99xc4x97xc4x8dxc4x8dxc4xafxc4x85'$final符合xc4x8dxc4x99xc4x97xc4x8dxc4x8dxc4xafxc4x85的条件,有了所有这些echo iconv('UTF-8','UTF-8',$final);应该解码和打印čęėččįą。但它不会解码,只打印$final的字符串,但是如果我将$final打印的字符串粘贴到iconv的字符串位置,它就可以完美地解码。为什么会这样?我该如何解决这个问题?谢谢。

这是我正在使用的Python脚本:

# -*- coding: utf-8 -*-
line="čęėččįą";
enc=line.encode('utf-8')
print (enc);

您需要将环境默认编码设置为utf-8。 一种方法是:

# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
line = "čęėččįą"
enc = line.encode('utf-8')
print (enc)

最新更新