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下的相互转换 ...
随机推荐
- 让Xcode的 stack trace信息可读
让Xcode的 stack trace信息可读 昨天在写 iOS 代码的时候,调试的时候模拟器崩溃了.异常停在了如下整个 main 函数的入口处: int main(int argc, char *a ...
- find命令详解
find命令详解 来源: ChinaUnix博客 日期: 2008.07.25 16:04 (共有条评论) 我要评论 [url=http://www.sudu.cn/web/host.php] ...
- HTML中忽略的小问题
1.padding和margin 例子 1 padding:10px 5px 15px 20px;(上,右,下,左) 上内边距是 10px 右内边距是 5px 下内边距是 15px 左内边距是 20p ...
- B-树的插入、查找、删除
转自:http://blog.163.com/zhoumhan_0351/blog/static/39954227200910231032917/ 前面讨论的查找都是内查询算法,被查询的数据都在内存. ...
- javaweb实验五
product类: package com.lab;public class Product { private int id; // 商品编号 private S ...
- Java实现智能机器自动操作电脑
package com.tz.util; import java.awt.Robot; import java.awt.event.InputEvent; import java.awt.event. ...
- 解决:Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3
[摘要:办理:org.apache.commons.net.MalformedServerReplyException: Could not parse response code.Server Re ...
- Struts 2.x No result defined for action 异常
这是我跑struts2的第一个例子,跑的也够郁闷的,这个问题烦了我几个钟... 2011-5-10 10:10:17 com.opensymphony.xwork2.util.logging.co ...
- app启动调用的api
(8)在app启动时,调用一个初始化api获取必要的信息 通过这个初始化api,获取一下必要的信息,例如,最新的app版本.当发现本地app的版本已经低于最新的app版本,可提示用户更新.当然了,这个 ...
- XHTML表单
1.HTML表单标记提供一套元素和属性,用来创建表单,收集网页参观者信息. 2.创建表单结构标记为<form>和</form>. 3.在<form>标记中,我们必须 ...