【发布时间】:2009-05-27 00:22:37
【问题描述】:
在 python3.0 中尝试tokenize 字符串时,为什么我会在标记开始之前得到前导'utf-8'?
从python3 docs 开始,tokenize 现在应该按如下方式使用:
g = tokenize(BytesIO(s.encode('utf-8')).readline)
但是,当在终端尝试此操作时,会发生以下情况:
>>> from tokenize import tokenize
>>> from io import BytesIO
>>> g = tokenize(BytesIO('foo'.encode()).readline)
>>> next(g)
(57, 'utf-8', (0, 0), (0, 0), '')
>>> next(g)
(1, 'foo', (1, 0), (1, 3), 'foo')
>>> next(g)
(0, '', (2, 0), (2, 0), '')
>>> next(g)
utf-8 标记在其他标记之前是怎么回事?这应该发生吗?如果是这样,那么我应该总是跳过第一个令牌吗?
[编辑]
我发现令牌类型 57 是 tokenize.ENCODING,如果需要,可以轻松地将其从令牌流中过滤掉。
【问题讨论】:
标签: python io tokenize bytesio