我使用的是 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'对象没有属性'next'python https://stackoverflow.com/questions/26967509/attributeerror-io-textiowrapper-object-has-no-attribute-next-python我仍然不知道如何让它在 Python 3.5 GUI shell 中工作。
到目前为止,我明白对于 Python 3.5 我必须使用.__next__
;对于Python 2.7.next
.
Use the next() function https://docs.python.org/3/library/functions.html#next在迭代器上:
cols = next(data).strip().split(',')
这是跨 Python 版本兼容的。
你确实可以交换.next()
for .__next__()
,但最好在这里使用标准函数,就像您使用的那样len(obj)
而不是打电话obj.__len__()
。双下划线方法是hooks由 Python 使用,您的代码应该使用可能会或可能不会调用这些钩子的标准 API。这里尤其如此,钩子名称发生了变化。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)