【Python】python2 str 编码检测


python2 str 编码检测

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)

# 
# {'confidence': 1.0, 'encoding': 'ascii'}
# 
# {'confidence': 0.938125, 'encoding': 'utf-8'}
# 
# {'confidence': 0.938125, 'encoding': 'utf-8'}