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

 

正文

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

一、read方法

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

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

file = open('兼职模特联系方式.txt', 'r')  # 创建的这个文件,也是一个可迭代对象

try:
text = file.read() # 结果为str类型
print(type(text))
print(text)
finally:
file.close()
"""
<class 'str'>
吴迪 177 70 13888888
王思 170 50 13988888
白雪 167 48 13324434
黄蓉 166 46 13828382
"""

  read()直接读取字节到字符串中,包括了换行符

>>> file = open('兼职模特联系方式.txt', 'r')
>>> a = file.read()
>>> a
'吴迪 177 70 13888888\n王思 170 50 13988888\n白雪 167 48 13324434\n黄蓉 166 46 13828382'

二、readline方法

  特点:readline()方法每次读取一行;返回的是一个字符串对象,保持当前行的内存

  缺点:比readlines慢得多

file = open('兼职模特联系方式.txt', 'r')

try:
while True:
text_line = file.readline()
if text_line:
print(type(text_line), text_line)
else:
break
finally:
file.close()
"""
<class 'str'> 吴迪 177 70 13888888 <class 'str'> 王思 170 50 13988888 <class 'str'> 白雪 167 48 13324434 <class 'str'> 黄蓉 166 46 13828382
"""

  readline()  读取整行,包括行结束符,并作为字符串返回

>>> file = open('兼职模特联系方式.txt', 'r')
>>> a = file.readline()
>>> a
'吴迪 177 70 13888888\n'

三、readlines方法

  特点:一次性读取整个文件;自动将文件内容分析成一个行的列表

file = open('兼职模特联系方式.txt', 'r')

try:
text_lines = file.readlines()
print(type(text_lines), text_lines)
for line in text_lines:
print(type(line), line)
finally:
file.close()
"""
<class 'list'> ['吴迪 177 70 13888888\n', '王思 170 50 13988888\n', '白雪 167 48 13324434\n', '黄蓉 166 46 13828382']
<class 'str'> 吴迪 177 70 13888888 <class 'str'> 王思 170 50 13988888 <class 'str'> 白雪 167 48 13324434 <class 'str'> 黄蓉 166 46 13828382
"""

  readlines()读取所有行然后把它们作为一个字符串列表返回。

>>> file = open('兼职模特联系方式.txt', 'r')
>>> a = file.readlines()
>>> a
['吴迪 177 70 13888888\n', '王思 170 50 13988888\n', '白雪 167 48 13324434\n', '黄蓉 166 46 13828382']

python中read()、readline()、readlines()函数的更多相关文章

  1. Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted

    1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...

  2. Python中的高阶函数与匿名函数

    Python中的高阶函数与匿名函数 高阶函数 高阶函数就是把函数当做参数传递的一种函数.其与C#中的委托有点相似,个人认为. def add(x,y,f): return f( x)+ f( y) p ...

  3. python中enumerate()函数用法

    python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...

  4. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  5. Python中sort和sorted函数代码解析

    Python中sort和sorted函数代码解析 本文研究的主要是Python中sort和sorted函数的相关内容,具体如下. 一.sort函数 sort函数是序列的内部函数 函数原型: L.sor ...

  6. Python中进制转换函数的使用

    Python中进制转换函数的使用 关于Python中几个进制转换的函数使用方法,做一个简单的使用方法的介绍,我们常用的进制转换函数常用的就是int()(其他进制转换到十进制).bin()(十进制转换到 ...

  7. Python学习之---Python中的内置函数(方法)(更新中。。。)

    add(item)   #将item添加到s中,如果item已经在s中,则无任何效果 break        #退出循环,不会再运行循环中余下的代码 bool()     #将参数转换为布尔型 by ...

  8. Python中的内置函数

    2.1 Built-in Functions The Python interpreter has a number of functions built into it that are alway ...

  9. python中判断readline读到文件末尾

    fp = open('somefile.txt') while True: line = fp.readline() if not line: #等价于if line == "": ...

  10. Python中str()与repr()函数的区别

    在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即str()或者 repr() . >>> a = 10 >>> type(str(a ...

随机推荐

  1. Django设置联合唯一约束 -- migrate时报错处理

    异常信息: a unique database constraint for 2 or more fields together 场景描述: 对于ORM中多对多关系的中间表,如果该关系表是手动创建的, ...

  2. ERROR tool.ImportTool: Encountered IOException running import job: java.io.IOException: Hive exited with status 2

    这是sqoop的 迁移数据到hive的报错 解决方案: 1,已经尝试不是  晚上大多数说的 libthrity的原因 2,查看自己的配置  sqoop-env.sh  如果配置的路径写的不对,对应的包 ...

  3. Android get current Locale, not default

    he default Locale is constructed statically at runtime for your application process from the system ...

  4. JavaScript -- Window-弹出窗口

    -----033-Window-弹出窗口.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=& ...

  5. 《垃圾回收的算法与实现》——Python垃圾回收

    Python垃圾回收 python采用引用计数法进行垃圾回收 Python内存分配 python在分配内存空间时,在malloc之上堆放了3个独立的分层. python内存分配时主要由arena.po ...

  6. 一学期积累下来的SQL语句写法的学习

    整合了一下上学期学习的积累,希望可以帮到初学者! 可能以后会有用吧! A 基本语句的运用 操作基于emp表1.按工资从高到低排列SQL> select rownum as 次序,ename,sa ...

  7. 如何做实时监控?—— 参考 Spring Boot 实现(转)

    转自:http://blog.csdn.net/xiaoyu411502/article/details/48129057 随着 微服务 的流行,相比较以前一个大型应用程序搞定所有需求,我们现在更倾向 ...

  8. quartz ? * 区别

    官方文档上提到问号时是这样说的: The '?' character is allowed for the day-of-month and day-of-week fields. It is use ...

  9. VC++记录

    1. 记录时间 #include <atlstr.h>#include <time.h>clock_t clockBegin, clockEnd; clockBegin = c ...

  10. 自制 Chrome Custom.css 设置网页字体为微软雅黑扩展

    自己做的將網頁自動替換為微軟雅黑的擴展.很好用. 將Customcss.rcx拖到擴展裏就可. 下載:Customcss.zip