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下的相互转换 ...
随机推荐
- js打印功能
1.window.print方式: jsp页面 打印按钮: <input type="button" value="打印" onclick=& ...
- Odoo 9 Odoo $ JQuery undifned
浏览器处于假死状态,查看console发现 odoo,jquery,$ 未定义三处错误,后台显示IOError: IOError: [Errno 2] No such file or director ...
- Hibernate调用存储过程和函数
操作大批量数据或复杂逻辑,考虑到执行效率和代码量的时候,存储过程和函数在数据库中是预编译好的,调用执行效率高 // 调用过程 {call 过程名称(?,?,?)} public static void ...
- 用一段JS代码来比较各浏览器的极限内存与运算速度
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- php 将查询出的数组数据存入redis
我们从数据库查询出来的数据一般为数组的形式, 而redis是不支持存入数组的, 一种解决办法是将数组转化为json数据,再将json存入redis,之后取出时再将json转化为php数组. 但将取出的 ...
- HDU3466 背包DP
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- C# 文件和文件夹操作
一.文件操作 1.File类的常用静态方法: void AppendAllText(string path, string contents),将文本contents附加到文件path中 bool E ...
- 2016HUAS暑假集训训练2 A - Is It A Tree?
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- MySQL Server-id踩到的坑
最近踩到一个说大不大,说小不小的坑,在此分享出来给各位同学.事情是这样的,线上有2台服务器,1主1从.A -> B,B服务器从A服务器同步数据.每天使用xtrabackup在B服务器上面进行全备 ...
- wxpython更新
.configure时候检查不到gtk+ 使用 apt-get install gnome-core-devel