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

2024-10-06 12:56:57 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我正在使用的插件:

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'\xc4\x8d\xc4\x99\xc4\x97\xc4\x8d\xc4\x8d\xc4\xaf\xc4\x85'$final等于\xc4\x8d\xc4\x99\xc4\x97\xc4\x8d\xc4\x8d\xc4\xaf\xc4\x85,因此echo iconv('UTF-8','UTF-8',$final);应该解码并打印čęėččįą。但它不解码,只打印$final的字符串,但如果我将$final打印的字符串粘贴到iconv的字符串位置,它就能完美地解码。为什么会这样?我该怎么解决这个问题?谢谢。你知道吗

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

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

Tags: path字符串echoexecuteapplicationresponseproc解码
1条回答
网友
1楼 · 发布于 2024-10-06 12:56:57

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

# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

line = "čęėččįą"
enc = line.encode('utf-8')
print (enc)

相关问题 更多 >