注意到Python每次读入一个文件的一行时,可以有两种写法:

f = open("bigFile.txt","r")

while True:

line = f.readline()

ifline:

pass# do something here

else:

break

f.close()

另一种写法为:

f = open("bigFile.txt","r")

forlineinf:

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每次读入文件一行的问题(血的教训啊)相关推荐

  1. 化学人学python有前途吗-转载:python之蟒开启理论计算化学的新时代

    转载:python之蟒开启理论计算化学的新时代 (2014-01-23 23:04:00) 标签: python gaussian python之蟒开启理论计算化学的新时代 就像iphone和andr ...

  2. python怎么读excelsheet_python3 excle(python怎么读写excel文件)

    python读取已经打开的3个word和excle文件的路径 用 win32com 操控 word和Excel就可以实现 咋样把python写入excle中 # 需安装 xlrd-0.9.2 和 xl ...

  3. python怎么读xlsx_用python读取xlsx文件

    一 准备工作: 二 xlrd库读取 首先安装xlrd库,安装方法: pip install xlrd import xlrd #打开excel wb = xlrd.open_workbook('tes ...

  4. python怎么读excel文件-python如何读写excel文件

    python读写excel的方式有很多,不同的模块在读写的方法上稍有区别: 用xlrd和xlwt进行excel读写: 用openpyxl进行excel读写: 用pandas进行excel读写: 为了方 ...

  5. python怎么读文件内容-Python读取文件内容为字符串的方法(多种方法详解)

    以下笔记是我在 xue.cn 学习群之数据分析小组所整理分享的心得.相关背景是:我选择中文词频统计案例作为考察大家python基础功掌握程度. 以小见大,下面是2个小技能的具体实战: 如何灵活地处理文 ...

  6. python怎么读excel文件-Python读写Excel文件方法介绍

    一.读取excel 这里介绍一个不错的包xlrs,可以工作在任何平台.这也就意味着你可以在Linux下读取Excel文件. 首先,打开workbook: 复制代码 代码如下: import xlrd ...

  7. format函数python的顺序_[转载] Python中format函数用法

    Python中format函数用法 format优点 format是python2.6新增的格式化字符串的方法,相对于老版的%格式方法,它有很多优点. 1.不需要理会数据类型的问题,在%方法中%s只能 ...

  8. python中await async_[转载]python中的await和async

    原文https://blog.csdn.net/Likianta/article/details/90123678 from time import sleep, time def demo1(): ...

  9. python怎么读excel文件-python读写excel文件

    项目中需要生成excel表格,在网上查了一些资料后,整理记录下. 1. 读excel表格 1 ''' 2 读取XLS,XLSX文件3 ''' 4 defreadExcelFile(filename): ...

最新文章

  1. 回复 集赞 抢 《Apple Watch 苹果开发教程》活动开始了!!!
  2. mac u盘文件过大 拷贝不进去_使用mac时文件太大无法拷贝怎么办
  3. 实现Singleton 模式——七种实现方式
  4. Knn算法(机器学习)入门(Python实现)
  5. 数据挖掘 pandas基础入门之查看数据
  6. 电子计算机 俗称 它是一种具有,2013年计算机一级B试题模拟1
  7. Spring 注入集合
  8. C# 设计模式 - 单例模式 演示
  9. Solve one floodlight install problem
  10. linux 磁盘分区对齐,linux查看硬盘4K对齐方法
  11. 矩阵理论第一章-特征值与特征向量
  12. 乔纳森 刘易斯 oracle,阿泰刘易斯都是幻想 专家点火箭引援之真命天子
  13. 第十一届蓝桥杯大赛软件类决赛(C/C++ 大学A组)
  14. 莫名其妙把电脑机箱左边耳机孔弄出来声音
  15. ios微信双开,iOS微信分身免越狱方法来了!
  16. 【苹果相册】苹果推信群发准入ProvisioningProfile还分为开发和分发
  17. Juc_并发编程目录
  18. weiphp2.0:关于OneThink后台添加密码重置的功能
  19. 遇到maven私服下载过慢或者卡死的情况
  20. 计算机病毒14款应用,14款游戏类APP隐私不合规:和平精英、开心消消乐等

热门文章

  1. 什么是main方法?
  2. android的消息处理机制(图+源码分析)——Looper,Handler,Message
  3. BizTalk学习笔记系列之二:实例说明如何使用BizTalk
  4. docker 删除默认连接_Net Core 中 Docker应用
  5. 下列不是python对文件的读操作方法是-小猿圈python之python期末考试测试题(一)...
  6. python学到什么程度可以做兼职-Python学到什么程度才可以去找工作?掌握这4点足够了!...
  7. 可以获取python整数类型帮助的是什么-python数据类型一(重点是字符串的各种操作)...
  8. ipython安装教程-ipython notebook教程
  9. python大数据分析实例-如何用Python分析大数据(以Twitter数据挖掘为例)
  10. 自学python的书籍逐级推荐-近300本Python书籍到底哪家强,用Python告诉你