read(),readline() 和 readlines() 比较
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() 比较的更多相关文章
- python文件读read()、readline()、readlines()对比
读取文件的三个方法:read().readline().readlines().均可接受一个变量用以限制每次读取的数据量,但通常不使用.本章目的是分析和总结三种读取方式的使用方法和特点. 一.read ...
- python读文件的三个方法read()、readline()、readlines()详解
文件 runoob.txt 的内容如下: 1:www.runoob.com2:www.runoob.com3:www.runoob.com4:www.runoob.com5:www.runoob.co ...
- python中read()、readline()、readlines()函数
python文件读read().readline().readlines()对比 目录 一.read方法 二.readline方法 三.readlines方法 正文 读取文件的三个方法:read( ...
- python第二十九课——文件读写(readline()和readlines()的使用)
演示readline()和readlines()的使用: #1.打开文件 f3=open(r'a.txt','r',encoding='gbk') #2.读取数据 content3=f3.readli ...
- python中read() readline()以及readlines()用法
[转自:http://www.ibm.com/developerworks/cn/linux/sdk/python/python-5/index.html#N1004E] 我们谈到“文本处理”时,我们 ...
- python中的三个读read(),readline()和readlines()
Python 将文本文件的内容读入可以操作的字符串变量非常容易. 文件对象提供了三个“读”方法: .read()..readline() 和 .readlines(). 每种方法可以接受一个变量以限制 ...
- python的readline() 和readlines()
.readline() 和 .readlines() 之间的差异是后者一次读取整个文件,象 .read() 一样..readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python ...
- 使用read、readline、readlines和pd.read_csv、pd.read_table、pd.read_fwf、pd.read_excel获取数据
从文本文件读取数据 法一: 使用read.readline.readlines读取数据 read([size]):从文件读取指定的字节数.如果未给定或为负值,则去取全部.返回数据类型为字符串(将所有行 ...
- Python - 文件读取read()、readline()、readlines()区别
前言 读取文件的三个方法:read().readline().readlines().均可接受一个方法参数用以限制每次读取的数据量,但通常不使用 read() 优点:读取整个文件,将文件内容放到一个字 ...
随机推荐
- Xcode7.1环境下上架iOS App到AppStore 流程③
前言部分 part三 部分主要讲解 Xcode关联绑定发布证书的配置.创建App信息.使用Application Loader上传.ipa文件到AppStore 一.Xcode配置发布证书信息 1)给 ...
- a标签指定的url,在表单提交前进行js验证的实现
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- hdu1166:敌兵布阵(树状数组或线段树)
题目描述: 一堆废话不用看...... 输入: 第一行一个整数T,表示有T组数据.每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表 ...
- 新建工程spring boot
新建工程spring boot 使用Maven管理, 在官网(http://atart.spring.io)下载demo后,加入依赖 <dependency> <gr ...
- php7结合mongoDB插入数据
php7结合mongoDB插入数据 代码如下: <?php $bulk = new MongoDB\Driver\BulkWrite;//1 $document = ['_id' => n ...
- Unity Shader之模板测试
Unity Shader之模板测试 一沙一世界,一花一天堂 一.Stencil testing 渲染管线 当片段着色器处理完一个片段之后,模板测试(Stencil Test)会开始执行,和深度 ...
- HttpCanary——最强Android抓包工具!
迎使用HttpCanary——最强Android抓包工具! HttpCanary是一款功能强大的HTTP/HTTPS/HTTP2网络包抓取和分析工具,你可以把他看成是移动端的Fiddler或者Char ...
- 1381. 删除 (Standard IO)
题目描述: Alice上化学课时又分心了,他首先画了一个3行N列的表格,然后把数字1到N填入表格的第一行,保证每个数只出现一次,另外两行他也填入数字1到N,但不限制每个数字的出现次数.Alice现在想 ...
- python 装饰器 第一步:基本函数
# 第一步:基本函数 def eat(): print('吃饭') # 调用 eat()
- 将QTP运行时的错误截图上传到QC
Class QCImageErrorCapture Sub Class_Terminate() 'Check if the current test has failed. If failed the ...