1
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import chardet
s = '123'.encode('utf-8')
print(s)
print(chardet.detect(s))

ss ='编程'.encode('utf-8')
print(chardet.detect(ss))

和结果

b'123'
{'encoding': 'ascii', 'confidence': 1.0, 'language': ''}
{'encoding': 'utf-8', 'confidence': 0.7525, 'language': ''}

为什么它不能检测s为 UTF-8?

为什么是ASCII?

这条线没用吗?# -*- coding: utf-8 -*- Python新人,谢谢!

4

1 回答 1

1

让我们谈谈这些台词——所有的肉都在那里:

s = '123'.encode('utf-8')
print(s)

您是正确的,Python 3 默认使用 Unicode。当您说'123'.encode()您正在将 Unicode 字符串转换为字节序列时,该字节序列将打印出丑陋的b前缀,以提醒您它不是默认的字符串类型。

于 2017-09-09T14:42:26.070 回答