我使用的是 Python 3.5,但这本书教的是 2.7(为什么在 2016 年打败了我)
使用 Python 学习预测分析,作者 Ashish Kumar,2016 年 2 月 15 日
>>> data=open(filename,'r')
>>> cols=data.next().strip().split(',')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
cols=data.next().strip().split(',')
AttributeError: '_io.TextIOWrapper' object has no attribute 'next'
>>>
我读过这个 AttributeError: '_io.TextIOWrapper' object has no attribute 'next' python 而且我仍然不知道如何让它在 Python 3.5 GUI shell 中工作。
到目前为止,我明白对于 Python 3.5 我必须使用 .__next__
;对于 Python 2.7 .next
.
最佳答案
使用 next()
function关于迭代器:
cols = next(data).strip().split(',')
这在 Python 版本之间是兼容的。
您确实可以将 .next()
换成 .__next__()
,但最好在此处使用标准函数,就像您使用 len(obj)
而不是调用 obj.__len__()
。双下划线方法是 Python 使用的 hooks,您的代码应该使用可能调用也可能不调用这些 hooks 的标准 API。此处尤其如此,钩子(Hook)名称已更改。
关于python - 如何将此代码从 Python 2.7 转换为 Python 3.5 以修复 ---> AttributeError : '_io.TextIOWrapper' object has no attribute 'next' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36420642/