read(),readline() 和 readlines() 比较

共同点:均可接受一个变量用以限制每次读取的数据量,但通常不使用

区别:

  read() 【即 fileObject().read( [size] )

    特点:读取整个文件,将文件内容放在一个字符串变量中。

    劣势:如果文件非常大,尤其大于内存时,无法使用read()方法。

with open(r'../learn_file/file_to_read.txt', encoding='utf-8', mode='r') as fb:
content = fb.read()
print(type(content))
print(content)
# 输出:
# <class 'str'>
# line 1: Hello, Mike.
# line 2: Nice to meet you. I'm Nick.
# line 3: Welcome to Shenzhen.
# line 4: Thx, it is really a beautiful city. I enjoy my time here.
# line 5: It is. Shall we go for some coffee this afternoon.
# line 6:Sure. And I want to discuss some details about the project we're going to work for

  readline() 【即 fileObject.readline( [size] ),[size]表示可选参数。】

    特点:从文件中一行一行地整行读取数据,如果指定了一个非负数的参数,则返回指定大小的字节数。

    缺点:比readlines()慢得多

with open(r'../learn_file/file_to_read.txt', encoding='UTF-8', mode='r+') as fb:
while True:
content = fb.readline().replace('\n', '')
# content = fb.readlines()
# if not content:
# break
if content:
print(type(content), content)
else:
break
# print(type(content))
# print(type(content), content)
print(fb.name)
# 输出:
# <class 'str'> line 1: Hello, Mike.
# <class 'str'> line 2: Nice to meet you. I'm Nick.
# <class 'str'> line 3: Welcome to Shenzhen.
# <class 'str'> line 4: Thx, it is really a beautiful city. I enjoy my time here.
# <class 'str'> line 5: It is. Shall we go for some coffee this afternoon.
# <class 'str'> line 6:Sure. And I want to discuss some details about the project we're going to work for.
# ../learn_file/file_to_read.txt

 readlines() 【即 fileObject.readlines( [sizeint] ),[sizeint] 表示可选参数】

    特点:从文件一次读取所有行并返回列表,若给定sizeint > 0,返回总和大约为sizeint字节的行

with open(r'../learn_file/file_to_read.txt', encoding='utf-8', mode='r') as fb:
content = fb.readlines()
print(type(content))
for line in content:
print(type(line), line.replace('\n', ''))
# 输出
# <class 'list'>
# <class 'str'> line 1: Hello, Mike.
# <class 'str'> line 2: Nice to meet you. I'm Nick.
# <class 'str'> line 3: Welcome to Shenzhen.
# <class 'str'> line 4: Thx, it is really a beautiful city. I enjoy my time here.
# <class 'str'> line 5: It is. Shall we go for some coffee this afternoon.
# <class 'str'> line 6:Sure. And I want to discuss some details about the project we're going to work for.

read(),readline() 和 readlines() 比较的更多相关文章

  1. python文件读read()、readline()、readlines()对比

    读取文件的三个方法:read().readline().readlines().均可接受一个变量用以限制每次读取的数据量,但通常不使用.本章目的是分析和总结三种读取方式的使用方法和特点. 一.read ...

  2. python读文件的三个方法read()、readline()、readlines()详解

    文件 runoob.txt 的内容如下: 1:www.runoob.com2:www.runoob.com3:www.runoob.com4:www.runoob.com5:www.runoob.co ...

  3. python中read()、readline()、readlines()函数

    python文件读read().readline().readlines()对比   目录 一.read方法 二.readline方法 三.readlines方法 正文 读取文件的三个方法:read( ...

  4. python第二十九课——文件读写(readline()和readlines()的使用)

    演示readline()和readlines()的使用: #1.打开文件 f3=open(r'a.txt','r',encoding='gbk') #2.读取数据 content3=f3.readli ...

  5. python中read() readline()以及readlines()用法

    [转自:http://www.ibm.com/developerworks/cn/linux/sdk/python/python-5/index.html#N1004E] 我们谈到“文本处理”时,我们 ...

  6. python中的三个读read(),readline()和readlines()

    Python 将文本文件的内容读入可以操作的字符串变量非常容易. 文件对象提供了三个“读”方法: .read()..readline() 和 .readlines(). 每种方法可以接受一个变量以限制 ...

  7. python的readline() 和readlines()

    .readline() 和 .readlines() 之间的差异是后者一次读取整个文件,象 .read() 一样..readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python ...

  8. 使用read、readline、readlines和pd.read_csv、pd.read_table、pd.read_fwf、pd.read_excel获取数据

    从文本文件读取数据 法一: 使用read.readline.readlines读取数据 read([size]):从文件读取指定的字节数.如果未给定或为负值,则去取全部.返回数据类型为字符串(将所有行 ...

  9. Python - 文件读取read()、readline()、readlines()区别

    前言 读取文件的三个方法:read().readline().readlines().均可接受一个方法参数用以限制每次读取的数据量,但通常不使用 read() 优点:读取整个文件,将文件内容放到一个字 ...

随机推荐

  1. onkeyup的使用(将输入值为非数字的字符替换为空)

    onkeyup:当输入值的键盘抬起时触发这个事件. 例如: onkeyup="this.value=this.value.replace(/\D/g,'') 这是个正则式验证,用来验证输入值 ...

  2. C#正则表达式将html代码中的所有img标签提取

    /// <summary> /// 取得HTML中所有图片的 URL. /// </summary> /// <param name="sHtmlText&qu ...

  3. Unity编程标准导引-3.4 Unity中的对象池

    本文为博主原创文章,欢迎转载.请保留博主链接http://blog.csdn.net/andrewfan Unity编程标准导引-3.4 Unity中的对象池 本节通过一个简单的射击子弹的示例来介绍T ...

  4. [CSU1806]Toll

    题目:Toll 传送门:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1806 题目简述:给定n个点m条有向边的有向图,每条边的花费是$b_i ...

  5. [CSP-S模拟测试73]题解

    A.小P的2048 作为一个看B哥玩了一个寒假的人这种题闭眼切好吧 模拟即可.程序模块化后直接复制粘贴. 说什么模拟不能复制粘贴的都没水平 #include<cstdio> #includ ...

  6. MSSQL数据库表结构无法更改

    工具->Designers-> 组织保存要求重新创建表的更改 -> 把这个钩去掉就可以了    

  7. MVC 入门

    MVC是什么? MVC是一个框架模式,它用于把应用程序的输入.处理和输出进行强制性的分开.使用MVC应用程序被分成三个核心部件:模型.视图.控制器.它们各自处理自己的任务.最典型的MVC就是JSP+S ...

  8. OpenCV2.4.8 + CUDA7.5 + VS2013 配置

    配置过程主要参考:https://initialneil.wordpress.com/2014/09/25/opencv-2-4-9-cuda-6-5-visual-studio-2013/ 1.为什 ...

  9. 抓包工具fiddler下载配置(一):下载/安装&信任证书

    简介 Fiddler一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,html,js,css等文件 ...

  10. Prim算法生成迷宫

    初始化地图 function initMaze(r,c){ let row = new Array(2 * r + 1) for(let i = 0; i < row.length; i++){ ...