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. vs code 使用技巧整理

    快捷键 Ctrl + Shift + F:在文件夹中搜索; Ctrl + Shift + P:命令面板; Ctrl + Shift + T:重新打开 关闭的编辑页面; Ctrl+Shift+PgUp/ ...

  2. hdu 5868:Different Circle Permutation 【Polya计数】

    似乎是比较基础的一道用到polya定理的题,为了这道题扣了半天组合数学和数论. 等价的题意:可以当成是给正n边形的顶点染色,旋转同构,两种颜色,假设是红蓝,相邻顶点不能同时为蓝. 大概思路:在不考虑旋 ...

  3. LinuxMySQL主从复制原理图

    主库开启dump线程 从bin-log中取出数据 从库开启io线程和sql线程   io线程不断从主库中的dump线程中那到最新的bin-log和io线程中的master-info的数据进行比较,如果 ...

  4. Angular JS - 9 - SeaJS加载js模块

    seajs加载模块的三种方式 1.seajs.use() 加载入口模块,类似于Java的main函数 2.require:      当在一个模块中需要用到其它模块时一般用require加载 1)   ...

  5. 10.18.1 linux文本编辑器vim

    vi和vim的区别 编辑一个文本时,vi不会显示颜色,而vim会显示颜色,vi 有点类似windows记事本,简单,那么就是vim复杂编辑器,功能复杂,高亮,自动缩进(写shell/python脚本用 ...

  6. SSM - 全局跨域处理

    这几天在开发中编写项目时需要前后端分离,刚好涉及到跨域这个问题,很早之前做项目时也用过,也是在网上找的列子,来源已经无处可寻了,若侵必删! 跨域问题一般出现两者服务器不同或者不同的端口上访问资源时会存 ...

  7. python最近邻分类器KNN算法

    1. KNN算法 邻近算法,或者说K最近邻(kNN,k-NearestNeighbor)分类算法是数据挖掘分类技术中最简单的方法之一.所谓K最近邻,就是k个最近的邻居的意思,说的是每个样本都可以用它最 ...

  8. LintCode之各位相加

    题目描述: 我的代码 public class Solution { /* * @param num: a non-negative integer * @return: one digit */ p ...

  9. javascript:void()的理解

    href="javascript:void(0);"本身没有任何危害啊,表示这是一个空链接.如果想在网页上用a标签,但又不想产生页面实际跳转动作,就可以这么做.下面是一些用法对比: ...

  10. Linux ftp安装

    ftp安装部分,操作步骤如下: 可以使用yum命令直接安装ftp # yum install vsftpd ftp服务的开启与关闭命令: 开启:# /bin/systemctl start vsftp ...