Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3:标记化库更改_Python_Python 3.x_Compatibility_Tokenize - Fatal编程技术网

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)。