Python 3:标记化库更改
根据这一点:,Python 3:标记化库更改,python,python-3.x,compatibility,tokenize,Python,Python 3.x,Compatibility,Tokenize,根据这一点:,tokenize.generate_tokens应该使用,而不是tokenize.tokenize 这在Python2.6中工作得非常好。但是它在Python 3中不再工作了: >>> a = list(tokenize.generate_tokens(io.BytesIO("1\n".encode()).readline)) Traceback (most recent call last): File "<stdin>", line 1, in
tokenize.generate_tokens
应该使用,而不是tokenize.tokenize
这在Python2.6
中工作得非常好。但是它在Python 3中不再工作了:
>>> a = list(tokenize.generate_tokens(io.BytesIO("1\n".encode()).readline))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.2/tokenize.py", line 439, in _tokenize
if line[pos] in '#\r\n': # skip comments or blank lines
根据文档,似乎是tokenize。tokenize
是使用此模块的新方法:<代码>标记化。生成\u标记甚至不再有文档记录
但是,如果没有文档记录,为什么在这个模块中仍然有一个generate_tokens
函数?我还没有找到任何关于这件事的鼓励
我正试图为
python2.5-3.2
维护一个代码库,我是否应该为python2
调用generate_tokens
,为python3
调用tokenize
?没有更好的方法吗?在python3中生成令牌
没有文档记录,但是。它是为了向后兼容,所以您可以使用它,但最好使用更改后的…generate_tokens
在python3中没有文档记录,但是。它是为了向后兼容,所以您可以使用它,但最好使用更改后的…generate\u tokens
在Python 3
中似乎真的很奇怪。它的工作方式与Python 2中的不同。但是,tokenize.tokenize
的行为类似于旧的python2
tokenize.generate_tokens
。因此,我写了一个小变通方法:
import tokenize
if sys.hexversion >= 0x03000000d:
tokenize_func = tokenize.tokenize
else:
tokenize_func = tokenize.generate_tokens
现在我只使用
tokenize\u func
,它工作起来没有问题。generate\u tokens
在python3
中似乎真的很奇怪。它的工作方式与Python 2中的不同。但是,tokenize.tokenize
的行为类似于旧的python2
tokenize.generate_tokens
。因此,我写了一个小变通方法:
import tokenize
if sys.hexversion >= 0x03000000d:
tokenize_func = tokenize.tokenize
else:
tokenize_func = tokenize.generate_tokens
现在我只使用
tokenize\u func
,它可以毫无问题地工作。更具体地说,Python 3中的tokenize()
需要以字节模式使用类似文件的对象generate_tokens
用于在文本模式下使用对象(即读取unicode)。更具体地说,Python 3中的tokenize()
需要在字节模式下使用类似文件的对象<代码>生成标记用于在文本模式下使用对象(即读取unicode)。