【Python】python2 str 编码检测

2023-10-17 09:54:57 浏览数 (3)

python2 str 编码检测

代码语言:javascript复制
import chardet

s = 'sdffdfd'
print type(s)
print chardet.detect(s)

s2 = '反反复复'
print type(s2)
print chardet.detect(s2)

s3 = u'反反复复'.encode('utf-8')
print type(s3)
print chardet.detect(s3)

# <type 'str'>
# {'confidence': 1.0, 'encoding': 'ascii'}
# <type 'str'>
# {'confidence': 0.938125, 'encoding': 'utf-8'}
# <type 'str'>
# {'confidence': 0.938125, 'encoding': 'utf-8'}

0 人点赞