转载 python每次读入文件一行的问题(血的教训啊)
2012-03-15 12:06
323 查看
/article/2959239.html
[python] view plaincopyf = open("bigFile.txt", "r")
while True:
line = f.readline()
if line:
pass # do something here
else:
break
f.close()
另一种写法为:
[python] view plaincopyf = open("bigFile.txt", "r")
for line in f:
pass # do something here
f.close()
很明显地,后一种写法更简洁,而且经过测试,效率似乎也略高一些,不过好像很少看见这样的写法。不知道这两种写法有什么区别。
刚刚看到PyDoc中的FileObject的next()说明
-----------------------------------------
next( )
A file object is its own iterator, for example iter(f) returns f
(unless f is closed). When a file is used as an iterator, typically in
a for loop (for example, for line in f: print line), the next() method
is called repeatedly. This method returns the next input line, or
raises StopIteration when EOF is hit when the file is open for reading
(behavior is undefined when the file is open for writing). In order to
make a for loop the most efficient way of looping over the lines of a
file (a very common operation), the next() method uses a hidden read-
ahead buffer. As a consequence of using a read-ahead buffer, combining
next() with other file methods (like readline()) does not work right.
However, using seek() to reposition the file to an absolute position
will flush the read-ahead buffer. New in version 2.3.
Python每次读入文件一行的问题
分类: Python2009-07-07 21:46 1612人阅读 评论(0) 收藏 举报 注意到Python每次读入一个文件的一行时,可以有两种写法:[python] view plaincopyf = open("bigFile.txt", "r")
while True:
line = f.readline()
if line:
pass # do something here
else:
break
f.close()
另一种写法为:
[python] view plaincopyf = open("bigFile.txt", "r")
for line in f:
pass # do something here
f.close()
很明显地,后一种写法更简洁,而且经过测试,效率似乎也略高一些,不过好像很少看见这样的写法。不知道这两种写法有什么区别。
刚刚看到PyDoc中的FileObject的next()说明
-----------------------------------------
next( )
A file object is its own iterator, for example iter(f) returns f
(unless f is closed). When a file is used as an iterator, typically in
a for loop (for example, for line in f: print line), the next() method
is called repeatedly. This method returns the next input line, or
raises StopIteration when EOF is hit when the file is open for reading
(behavior is undefined when the file is open for writing). In order to
make a for loop the most efficient way of looping over the lines of a
file (a very common operation), the next() method uses a hidden read-
ahead buffer. As a consequence of using a read-ahead buffer, combining
next() with other file methods (like readline()) does not work right.
However, using seek() to reposition the file to an absolute position
will flush the read-ahead buffer. New in version 2.3.
相关文章推荐
- Python每次读入文件一行的问题
- python读取CSV文件以及“写入CSV致文件每一行后面会多一个空行”问题解决
- 【转载】python计算文件的行数和读取某一行内容的实现方法
- 读写文件:每次读入大文件里的一行、读写.CSV文件
- 【C语言】从文件每次读入一行字符串,并把这些字符串添加到字符串数组中
- python中读取txt文件,windows下么有毛病,到Linux下总是只有最后一行有效,怎么解决?
- shell读文件时无法读入tab的问题
- 金币阵列问题。有m*n枚金币在桌面上排成一个金币阵列。每一个金币正面朝上,或背面朝上,分别用0和1表示。 金币阵列游戏的规则是:(1)每次可将任一行金币翻过来放在原来的位置上;(2)每
- Python模块函数:os.listdir 打印Win7目录中的文件-中文乱码问题
- <转载>正确处理下载文件时HTTP头的编码问题(Content-Disposition)
- [python学习]列表合并的问题(转载)
- 解决jsp无法读入css、js等文件的问题
- python文件录入编码的问题
- 吐血解决python中文写入文件问题
- Python遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例
- 五个python面试问题(转载)https://segmentfault.com/a/1190000000618513
- iOS 制作静态库 .a 文件 和 .framework 文件 以及解决第三方库冲突问题 最后附上图片及Xib资源打包成 bundle (转载 mark 一下)
- Python 2.7 解决写入文件的中文乱码问题
- python2的编码问题(因split处理文件深入的)
- Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题