【问题标题】:Why doesnt this plugin decode $final?为什么这个插件不解码 $final?
【发布时间】:2016-10-16 22:35:18
【问题描述】:

这是我正在使用的插件:

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);

【问题讨论】:

    标签: php python decoding


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多