\u, \x,0x区别

\u 字符串

import json

# 编码为json字符串
json_str = json.dumps(“你好”)  # "\\u4f60\\u597d"
print(json_str)  # '\u4f60\u597d'

# 解码为python对象
data = json.loads(json_str) # “你好”
print(data) # 你好

s = '\u4f60\u597d'
repr(s) # "你好"
str(s)  # 你好
print(s)  # 你好

\x字符串

s = '你好'.encode('utf-8') # b'\xe4\xbd\xa0\xe5\xa5\xbd'
s1 = '你好'.encode('gbk') # b'\xc4\xe3\xba\xc3'

s.decode('utf-8') # ‘ 你好’
s1.decode('gbk')  # '你好'

检测类型

pip install chardet

import chardet
s = '你好'.encode()
print(chardet.detect(s)) # {'encoding': 'utf-8', 'confidence': 0.7525, 'language': ''}

s = b'\u4f60\u597d'
print(chardet.detect(s))  #{'encoding': 'ascii', 'confidence': 1.0, 'language': ''}

s =  b'\xe4\xbd\xa0\xe5\xa5\xbd'
print(chardet.detect(s)) # {'encoding': 'utf-8', 'confidence': 0.7525, 'language': ''}

s = b'\xc4\xe3\xba\xc3'
print(chardet.detect(s)) # {'encoding': 'TIS-620', 'confidence': 0.3598212120361634, 'language': 'Thai'}

0x\x区别:

- 0x 表示整型数值(十六进制)。
	 0x41 = 65  
	 0o41 = 33 
	 0b101 = 5
-'\x' 表示字符串表达(带引号)
	'\x41' 表示 'A'
	'\x42' 表示 'B'
- \ 表示转义

\x 后边跟两位,表示单字节编码。
\d 十进制编码
\o 8进制编码

\u 1
chr
str
ord

【注】 Python2 的unicode 函数在 Python3 中被命名为 str。在 Python3 中使用 ·str 来代替 Python2 中的 unicode。

\u 代表 unicode 编码,是一个字符
\x 代表 UTF-8 编码的数据,可以转换为 Unicode 编码,得到对应的汉字,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值