微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

readlines是否在Python 3中返回列表或迭代器?

如何解决readlines是否在Python 3中返回列表或迭代器?

像这样:

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('/junk/so/foo.txt')
>>> type(f.readlines())
<class 'list'>
>>> help(f.readlines)
Help on built-in function readlines:

readlines(...)
    Return a list of lines from the stream.

    hint can be specified to control the number of lines read: no more
    lines will be read if the total size (in bytes/characters) of all
    lines so far exceeds hint.

>>>

解决方法

我在“深入Python 3”中已经读到“ readlines()方法现在返回一个迭代器,因此它的效率与Python 2中的xreadlines()一样。”
参见此处:http : //diveintopython3.org/porting-code-to-
python-3-with-2to3.html。我不确定这是真的,因为他们在这里没有提及:http
:
//docs.python.org/release/3.0.1/whatsnew/3.0.html。我该如何检查?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。