convert \uXXXX String to Unicode Characters in Python3.x
转换\uXXXX
if Python3.x:
str.decodeno longer exists in 3.x. that']s whyPython 3.4: str : AttributeError: 'str' object has no attribute 'decodeis thrown.- Unicode literal string
'\uxxxx\uxxxx'is different from string'\uxxxx\uxxxx'.
if you don't understand what liternal means, check the py3.x ducumentation
./descape.py '\u627e\u4e0d\u5230\u8be5\u8bcd\u7684\u89e3\u91ca'
#!/usr/bin/env python3
# file : descape.py
# convert the escaped chars like `\u45e3` to unicode
import sys, re
def h2d(a):
if len(a) != 4:
return False
j = 16 ** 3
r = 0
for i in range(0,len(a)):
b = ord(a[i])- 48
r += (b-39 if b > 9 else b) * j
j //= 16
return chr(r)
text = sys.argv[1]
# text is string. not unicode literals
def descape(utext):
o = ''
for ac in re.split(r'\\u([a-f0-9]{4})',text):
if not ac or len(ac) != 4:
continue
cur = ac
o += h2d(cur)
return o
print(descape(text))
json module
json.dumps()和json.dump()有一个参数ensure_ascii默认是True,改为False就不会把汉字编码成\uxxxx了
References:
convert \uXXXX String to Unicode Characters in Python3.x的更多相关文章
- Convert CString to ANSI string in UNICODE projects
Convert CString to ANSI string in UNICODE projects Quick Answer: use an intermediate CStringA. Norma ...
- Convert a string into an ArrayBuffer
https://github.com/mdn/dom-examples/blob/master/web-crypto/import-key/spki.js How to convert ArrayBu ...
- How to convert a QString to unicode object in python 2?
How to convert a QString to unicode object in python 2? I had this problem to solve, and I tried to ...
- 【原创】利用typeface实现不同字体的调用显示及String转换为Unicode
最近工作用到,就写个小demo demo实现从assets中利用typeface调用不同字体,并在editText中显示出来 1.layout中创建activity_main.xml文件 布局代码如下 ...
- emoji Unicode characters
http://www.easyapns.com/iphone-emoji-alerts he complete list of iPhone emoji Unicode characters. Jus ...
- Error: The INF file contains Unicode characters that could not be converted correctly
昨天第一次为自己的windows mobile程序制作CAB安装包,但是在生成过程中,却出现了这样一个问题: 编译完成 -- 0 个错误,0 个警告time -> G:\WindowsMobil ...
- pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode"
pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode" 经查询, 看到 ...
- Convert.ToInt32(string '000000003') 变成了 3
Convert.ToInt32(string '000000003') 变成了 3 但是在查询的时候需要用的是string 这里的convert.toint32 反而起了坏作用,不是所有的时候都要用c ...
- CString和string在unicode与非unicode下的相互转换(转)
原文转自 http://blog.csdn.net/u014303844/article/details/51397556 CString和string在unicode与非unicode下的相互转换 ...
随机推荐
- visual studio 2012 的制作ActiveX、打包和发布[转]
开发环境是Vs 2012 Framework 4.0 源地址:http://www.cnblogs.com/li-peng/p/3455247.html 源码和制作工具在文章最下边 一. Activ ...
- 在VS2012/2013上编辑和调试Quick-cocos2d-x的Lua代码
类似教程链接:http://www.cocoachina.com/bbs/read.php?tid=205043 步骤: 一.Quick-cocos2d-x 最新版本或要打算使用的版本 (http:/ ...
- requirejs解决异步模块加载方案
他首先会遍历enableRegistry取出其中定义的模块,并且将没有加载成功的模块标识注入noLoads数组,如果过期了这里就会报错 如果上述没问题还会做循环依赖的判断,主要逻辑在breakCycl ...
- How to bind data to a user control
http://support.microsoft.com/kb/327413 Create a user control by inheriting from the System.Windows. ...
- 在eclipse中将SVN项目check下来的正确步骤
学习下面的方法后再也不用从svncheck到本地后再导入到eclipse里了. 1. 首先Import,在弹出框里选择SVN-从SVN检出项目,然后按照提示一步一步直到选中了目标项目,然后点击next ...
- Postgre cannot insert multiple commands into a prepared statement
悲剧... FireDAC连接Postgre数据库, 使用默认的属性, 一次执行多条SQL的时候, 会报"cannot insert multiple commands into a pre ...
- jQuery/Javascript 事件停止冒泡
Demo: <div id='wrap'> <button id='btn'>btn</button> </div> 一般情况下,两个元素分别添加点击事 ...
- python3中urllib2的问题
import urllib from urllib import request a = urllib.request.Request(url) b = urllib.request.urlopen( ...
- NSString(或者说是UILabel)加入 “行间距” 之后的 “高度”计算
一.首先,写一个工具类(NSString的分类,增加两个功能,计算高度宽度) #import "NSString+Extension.h" @implementation NSSt ...
- c# word 转pdf 导出失败,因为此功能尚未安装
savePDF应该是office2007以上的版本才支持的,而且必须是完整版的office. 如果2007提示这个错误,还需要安装一个插件 http://download.microsoft.com/ ...