GVKun编程网logo

在python 3中解码(unicode_escape)一个字符串(python对字符串解码)

26

对于在python3中解码感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍unicode_escape一个字符串,并为您提供关于PHP–json_encode(字符串,JSON_UNESCAPE

对于在python 3中解码感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍unicode_escape一个字符串,并为您提供关于PHP – json_encode(字符串,JSON_UNESCAPED_UNICODE)没有逃避捷克字符、PHP解密Unicode及Escape加密字符串,unicodeescape、PHP解密Unicode及Escape加密字符串,unicodeescape_PHP教程、Python 中 ''unicodeescape'' codec can''t decode bytes in position XXX: trun错误解的有用信息。

本文目录一览:

在python 3中解码(unicode_escape)一个字符串(python对字符串解码)

在python 3中解码(unicode_escape)一个字符串(python对字符串解码)

我已经检查了此解决方案,但在python3中不起作用。

我有一个这样的转义字符串:str = "Hello\\nWorld"而且我想获得未转义的相同字符串:str_out = Hello\nWorld

我尝试了这个没有成功: AttributeError: 'str' object has no attribute 'decode'

这是我的示例代码:

str = "Hello\\nWorld"
str.decode('unicode_escape')

PHP – json_encode(字符串,JSON_UNESCAPED_UNICODE)没有逃避捷克字符

PHP – json_encode(字符串,JSON_UNESCAPED_UNICODE)没有逃避捷克字符

我正在从数据库中选择一些数据并将它们编码为json,但我遇到了类似捷克标志的问题

á,í,ř,č,ž…

我的文件是utf-8编码,我的数据库也是utf-8编码,我也设置了头文件到utf-8编码.我还应该做什么?

我的代码:

header('Content-Type: text/html; charset=utf-8');
while($tmprow = MysqLi_fetch_array($result)) {
        $row['user'] = mb_convert_encoding($tmprow['user'], "UTF-8", "auto");
        $row['package'] = mb_convert_encoding($tmprow['package'], "UTF-8", "auto");
        $row['url'] = mb_convert_encoding($tmprow['url'], "UTF-8", "auto");
        $row['rating'] = mb_convert_encoding($tmprow['rating'], "UTF-8", "auto");

        array_push($response, $row);
    }

    $json = json_encode($response, JSON_UnesCAPED_UNICODE);

    if(!$json) {
        echo "error";
    }

和印刷的json的一部分:“包”:“zv ??? tkanalouce”

编辑:没有mb_convert_encoding()函数,打印的字符串为空,并打印“错误”.

解决方法:

使用您在示例中获得的代码,输出为:

json_encode($response, JSON_UnesCAPED_UNICODE);
"package":"zv???tkanalouce"

你在那里看到了问号,因为它们是由mb_convert_encoding引入的.当您使用编码检测(“auto”作为第三个参数)并且编码检测无法处理输入中的字符,将其替换为问号时,会发生这种情况.示例性代码行:

$row['url'] = mb_convert_encoding($tmprow['url'], "UTF-8", "auto");

这也意味着来自数据库的数据不是UTF-8编码的,因为mb_convert_encoding($buffer,’UTF-8′,’auto’);如果$buffer是UTF-8编码,则不会引入问号.

因此,您需要找出数据库连接中使用的字符集,因为数据库驱动程序会将字符串转换为连接的编码.

最简单的是你只需告诉每个数据库链接你要求UTF-8字符串然后只使用它们:

$MysqLi = new MysqLi("localhost", "my_user", "my_password", "test");

/* check connection */
if (MysqLi_connect_errno()) {
    printf("Connect Failed: %s\n", MysqLi_connect_error());
    exit();
}

/* change character set to utf8 */
if (!$MysqLi->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $MysqLi->error);
} else {
    printf("Current character set: %s\n", $MysqLi->character_set_name());
}

前面的代码示例演示了如何使用MysqLi将默认客户端字符集设置为UTF-8.它已经是taken from the manual,也可以看到我们现场的材料,例如: utf 8 – PHP and MySQLi UTF8.

然后,您可以大大改善您的代码:

$response = $result->fetch_all(MysqLI_ASSOC);

$json = json_encode($response, JSON_UnesCAPED_UNICODE);

if (FALSE === $json) {
    throw new LogicException(
        sprintf('Not json: %d - %s', json_last_error(), json_last_error_msg())
    );
}

header('Content-Type: application/json'); 
echo $json;

PHP解密Unicode及Escape加密字符串,unicodeescape

PHP解密Unicode及Escape加密字符串,unicodeescape

php解密unicode及escape加密字符串,unicodeescape

本文给大家分享一个php解密unicode及escape加密字符串函数

<&#63;php  
function uni_decode($s) {  
  preg_match_all(''/\&\#([0-9]{2,5})\;/'', $s, $html_uni);  
  preg_match_all(''/[\\\%]u([0-9a-f]{4})/ie'', $s, $js_uni);  
  $source = array_merge($html_uni[0], $js_uni[0]);  
  $js = array();  
  for($i=0;$i<count($js_uni[1]);$i++) {  
    $js[] = hexdec($js_uni[1][$i]);  
  }  
  $utf8 = array_merge($html_uni[1], $js);  
  $code = $s;  
  for($j=0;$j<count($utf8);$j++) {  
    $code = str_replace($source[$j], unicode2utf8($utf8[$j]), $code);  
  }  
  return $code;//$s;//preg_replace(''/\\\u([0-9a-f]{4})/ie'', "chr(hexdec(''\\1''))", $s);  
}  
  
function unicode2utf8($c) {  
  $str="";  
  if ($c < 0x80) {  
     $str.=chr($c);  
  } else if ($c < 0x800) {  
     $str.=chr(0xc0 | $c>>6);  
     $str.=chr(0x80 | $c & 0x3f);  
  } else if ($c < 0x10000) {  
     $str.=chr(0xe0 | $c>>12);  
     $str.=chr(0x80 | $c>>6 & 0x3f);  
     $str.=chr(0x80 | $c & 0x3f);  
  } else if ($c < 0x200000) {  
     $str.=chr(0xf0 | $c>>18);  
     $str.=chr(0x80 | $c>>12 & 0x3f);  
     $str.=chr(0x80 | $c>>6 & 0x3f);  
     $str.=chr(0x80 | $c & 0x3f);  
  }  
  return $str;  
}  
  
$str=''%u5927%u5BB6%u597D%uFF0C我是孤魂!<br />\u8FD9\u662F\u6D4B\u8BD5\u6587\u672C\uFF01'';  
echo uni_decode($str); // 大家好,我是孤魂!这是测试文本!  
登录后复制

在网上搜索一把,很多用php实现的escape函数,大同小异

function phpescape($str){ 
  preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$newstr); 
  $ar = $newstr[0]; 
  foreach($ar as $k=>$v){ 
    if(ord($ar[$k])>=127){ 
      $tmpString=bin2hex(iconv("GBK","ucs-2",$v)); 
      if (!eregi("WIN",PHP_OS)){ 
        $tmpString = substr($tmpString,2,2).substr($tmpString,0,2); 
      } 
      $reString.="%u".$tmpString; 
    } else { 
      $reString.= rawurlencode($v); 
    } 
  } 
  return $reString; 
} 
登录后复制

以上所述就是本文的全部内容了,希望大家能够喜欢。

PHP解密Unicode及Escape加密字符串,unicodeescape_PHP教程

PHP解密Unicode及Escape加密字符串,unicodeescape_PHP教程

php解密unicode及escape加密字符串,unicodeescape

本文给大家分享一个php解密unicode及escape加密字符串函数

<&#63;php  
function uni_decode($s) {  
  preg_match_all(''/\&\#([0-9]{2,5})\;/'', $s, $html_uni);  
  preg_match_all(''/[\\\%]u([0-9a-f]{4})/ie'', $s, $js_uni);  
  $source = array_merge($html_uni[0], $js_uni[0]);  
  $js = array();  
  for($i=0;$i<count($js_uni[1]);$i++) {  
    $js[] = hexdec($js_uni[1][$i]);  
  }  
  $utf8 = array_merge($html_uni[1], $js);  
  $code = $s;  
  for($j=0;$j<count($utf8);$j++) {  
    $code = str_replace($source[$j], unicode2utf8($utf8[$j]), $code);  
  }  
  return $code;//$s;//preg_replace(''/\\\u([0-9a-f]{4})/ie'', "chr(hexdec(''\\1''))", $s);  
}  
  
function unicode2utf8($c) {  
  $str="";  
  if ($c < 0x80) {  
     $str.=chr($c);  
  } else if ($c < 0x800) {  
     $str.=chr(0xc0 | $c>>6);  
     $str.=chr(0x80 | $c & 0x3f);  
  } else if ($c < 0x10000) {  
     $str.=chr(0xe0 | $c>>12);  
     $str.=chr(0x80 | $c>>6 & 0x3f);  
     $str.=chr(0x80 | $c & 0x3f);  
  } else if ($c < 0x200000) {  
     $str.=chr(0xf0 | $c>>18);  
     $str.=chr(0x80 | $c>>12 & 0x3f);  
     $str.=chr(0x80 | $c>>6 & 0x3f);  
     $str.=chr(0x80 | $c & 0x3f);  
  }  
  return $str;  
}  
  
$str=''%u5927%u5BB6%u597D%uFF0C我是孤魂!<br />\u8FD9\u662F\u6D4B\u8BD5\u6587\u672C\uFF01'';  
echo uni_decode($str); // 大家好,我是孤魂!这是测试文本!  
登录后复制

在网上搜索一把,很多用php实现的escape函数,大同小异

function phpescape($str){ 
  preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$newstr); 
  $ar = $newstr[0]; 
  foreach($ar as $k=>$v){ 
    if(ord($ar[$k])>=127){ 
      $tmpString=bin2hex(iconv("GBK","ucs-2",$v)); 
      if (!eregi("WIN",PHP_OS)){ 
        $tmpString = substr($tmpString,2,2).substr($tmpString,0,2); 
      } 
      $reString.="%u".$tmpString; 
    } else { 
      $reString.= rawurlencode($v); 
    } 
  } 
  return $reString; 
} 
登录后复制

以上所述就是本文的全部内容了,希望大家能够喜欢。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1000260.htmlTechArticlePHP解密Unicode及Escape加密字符串,unicodeescape 本文给大家分享一个PHP解密Unicode及Escape加密字符串函数 php function uni_decode($s) { preg_match_all(''/\/...

Python 中 ''unicodeescape'' codec can''t decode bytes in position XXX: trun错误解

Python 中 ''unicodeescape'' codec can''t decode bytes in position XXX: trun错误解

Python 中 ‘unicodeescape’ codec can’t decode bytes in position XXX: trun错误解决方案

背景描述

今天在运用Python pillow模块处理图片时遇到一个错误

SyntaxError: (unicode error) ''unicodeescape'' codec can''t decode bytes in position 2-3: truncated \UXXXXXXXX escape
  • 1

刚开始以为是图片名字有中文,不识别,于是在python文件的头部加上

#-*- coding:utf-8 -*-
  • 1

但是加完这个还是报错,然后我就把图片的中文去掉还成英文,然后报错,一脸懵逼呀。后来在stackoverflow 上找到了类似的错误,原来是图片路径写的 有问题,错误代码如下

im = Image.open(''C:\Users\FrankYuan\Pictures\Camera Roll\WIN_20161010_08_51_57_Pro.jpg'')
  • 1

正确结果

im = Image.open(''C:\\Users\\FrankYuan\\Pictures\\Camera Roll\\WIN_20161010_08_51_57_Pro.jpg'')
  • 1

或者

im = Image.open(r''C:\Users\FrankYuan\Pictures\Camera Roll\WIN_20161010_08_51_57_Pro.jpg'')
  • 1

抑或

im = Image.open(r''C:/Users/FrankYuan/Pictures/Camera Roll/WIN_20161010_08_51_57_Pro.jpg'')
  • 1

原因:

window 读取文件可以用\,但是在字符串中\是被当作转义字符来使用,所以’d:\a.txt’会被转义成’d:\a.txt’这是正确路径,所以不会报错。而‘C:\Users\FrankYuan\Pictures\Camera Roll\WIN_20161010_08_51_57_Pro.jpg ’中经过转义之后可能就找不到路径的资源了,例如\t可能就转义成tab键了。

解决办法

python在描述路径时可以有多种方式,现列举常见的三种

方式一:转义的方式

''d:\\a.txt''

方式二:显式声明字符串不用转义

''d:r\a.txt''

方式三:使用Linux的路径/

''d:/a.txt''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

我强烈推荐第三种写法,这在Linux和window下都是行的通的。

我们今天的关于在python 3中解码unicode_escape一个字符串的分享就到这里,谢谢您的阅读,如果想了解更多关于PHP – json_encode(字符串,JSON_UNESCAPED_UNICODE)没有逃避捷克字符、PHP解密Unicode及Escape加密字符串,unicodeescape、PHP解密Unicode及Escape加密字符串,unicodeescape_PHP教程、Python 中 ''unicodeescape'' codec can''t decode bytes in position XXX: trun错误解的相关信息,可以在本站进行搜索。

本文标签: