版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/zhrq95/article/details/79300411

• read:如果指定了参数 size,就按照该指定长度从文件中读取内容,否则,就读取全文。被读出来的内容,全部塞到一个字符串里面。这样有好处,就是东西都到内存里面了,随时取用;但如果文件内容太多了,内存会吃不消

• readline:可选参数 size 的含义同上。它是以行为单位返回字符串,也就是每次读一行,依次循环,如果不限定 size,直到最后一个返回的是空字符串,意味着到文件末尾了(EOF)。

• readlines:size 同上。它返回的是以行为单位的列表,即相当于先执行 readline() ,得到每一行,然后把这一行的字符串作为列表中的元素塞到一个列表中,最后将此列表返回。

分别用上述三种函数读取这个文件:

一、read

>>> f = open(“you.md”)
>>> content = f.read()
>>> content
‘You Raise Me Up\nWhen I am down and, oh my soul, so weary;\nWhen troubles come and my heart burdened be;\nThen, ……(此处省略)’
>>> f.close()

得到的就是一个很大的字符串,因为原文中有换行,所以这个字符串里面包含着一些符号 \n 。如果用 print 输出这个字符串,其中的\n 起作用了:

>>> print content
You Raise Me Up
When I am down and, oh my soul, so weary;
When troubles come and my heart burdened be;
Then, I am still and wait here in the silence,
Until you come and sit awhile with me.
You raise me up, so I can stand on mountains;
You raise me up, to walk on stormy seas;
I am strong, when I am on your shoulders;
You raise me up: To more than I can be.

二、readline()

>>> f = open(“you.md”)
>>> f.readline()
‘You Raise Me Up\n’
>>> f.readline()
‘When I am down and, oh my soul, so weary;\n’
>>> f.readline()
‘When troubles come and my heart burdened be;\n’
>>> f.close()

每操作一次f.readline() ,就读取一行,并且将指针向下移动一行,如此循环。这是一种循环/可迭代。因此,可以用循环语句来完成对全文的读取。

#!/usr/bin/env Python
# coding=utf-8
f = open(“you.md”)
while True:
line = f.readline()
if not line: #到 EOF,返回空字符串,则终止循环
break
print line ,
f.close()

三、readlines()

>>> f = open(“you.md”)
>>> content = f.readlines()
>>> content
[‘You Raise Me Up\n’, ‘When I am down and, oh my soul, so weary;\n’, ‘When troubles come and my heart burdened be;\n’,….(此处省略)]

返回的是一个列表,列表中每个元素都是一个字符串,每个字符串中的内容就是文件的一行文字,含行末的符号。

它是可以用 for 来循环的
>>> for line in content:
… print line ,

You Raise Me Up
When I am down and, oh my soul, so weary;
When troubles come and my heart burdened be;
Then, I am still and wait here in the silence,
Until you come and sit awhile with me.
You raise me up, so I can stand on mountains;
You raise me up, to walk on stormy seas;
I am strong, when I am on your shoulders;
You raise me up: To more than I can be.
>>> f.close()

四、读取大文件

如果文件太大,就不能用 read() 或者 readlines() 一次性将全部内容读入内存,可以使用 while 循环和 readlin() 来完成这个任务。

此外,还可以使用 fileinput 模块:
>>> import fileinput
>>> for line in fileinput.input(“you.md”):
… print line ,

You Raise Me Up
When I am down and, oh my soul, so weary;
When troubles come and my heart burdened be;
Then, I am still and wait here in the silence,
Until you come and sit awhile with me.
You raise me up, so I can stand on mountains;
You raise me up, to walk on stormy seas;
I am strong, when I am on your shoulders;
You raise me up: To more than I can be.

还有一种方法,更为常用:
>>> for line in f:
… print line ,

You Raise Me Up
When I am down and, oh my soul, so weary;
When troubles come and my heart burdened be;
Then, I am still and wait here in the silence,
Until you come and sit awhile with me.
You raise me up, so I can stand on mountains;
You raise me up, to walk on stormy seas;
I am strong, when I am on your shoulders;
You raise me up: To more than I can be.
之所以能够如此,是因为 file 是可迭代的数据类型,直接用 for 来迭代即可

Python 读取文本时的 read/readline/readlines相关推荐

  1. python 读取文本及 read()、readline() 和 readlines()区别

     读文件 1.打开一个文件用open()方法   (open()返回一个文件对象,它是可迭代的): >>> f = open('test.txt', 'r') 注意:r表示是文本 ...

  2. python 读取特定一段文本_python提取文本内容 python读取文本每行指定内容

    用"python"怎么提取文件里的指定内容?原来这些年,他痊愈的只是外表,有一种伤,它深入骨髓,在人看不见的地方肆虐. python读取文件内容的方法: 一.最方便的方法是一次性读 ...

  3. python读文件一次读特定行_Python3实现从文件中读取指定行的方法 python读取文本内每行指定内容...

    如何用python读取文本中指定行的内容在这个世界上说不出口的话太多了,你能不能陪小编去,你能不能留下来,你能不能帮帮小编,你对小编很重要,所以你可不可以不要走,到最后哽咽出口的却是,没关系,小编可以 ...

  4. 如何用python读取文本中指定行的内容

    如何用python读取文本中指定行的内容 搜索资料 我来答 分享 新浪微博 QQ空间 浏览 5284 次 查看全文 http://www.taodudu.cc/news/show-64036.ht ...

  5. Python读取文件时出现UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x80 in position xx: 解决方案

    Python读取文件时出现UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position xx: 解决方案 参考文章: (1)Py ...

  6. python excel写入日期变数字_解决python 读取excel时 日期变成数字并加.0的问题

    excel 文件内容如下: 读取excel内容: import xlrd from datetime import datetime from xlrd import xldate_as_dateti ...

  7. python读取文本数据绘制曲线图

    目录 写在前面 代码 reference 写在前面 1.本文内容 python读取文本数据曲线图 2.转载请注明出处: https://blog.csdn.net/qq_41102371/articl ...

  8. Python读取文本的三种方式对比

    1.概述 Python有三种读取文本的方式,分别是: read() readline() readlines() 2.三种方式的优缺点分析 2.1 read() 最简单的一种方法,一次性读取文件的所有 ...

  9. python读取文本数据--完善中

    目录 小细节 传送门 Mat运算 readline,readlines split,strip '''导入数据input: file_name(string):文件的存储位置output: featu ...

最新文章

  1. MapReduce 中 UDF、UDAF、UDTF
  2. 车辆密度估计--Understanding Traffic Density from Large-Scale Web Camera Data
  3. Linux内存中的 buffer 和 cache 到底是个什么东东?
  4. 求数组中的最小值以及最小值的序列号
  5. 如何编写高质量CSS
  6. 如何生成安全的密码 Hash:MD5, SHA, PBKDF2, BCrypt 示例
  7. 时间管理神器:滴答清单之我最喜欢的特征
  8. 【Java基础】HashMap原理详解
  9. 数据库增删改查性能对比
  10. tostring、(string)和 String.valueOf()
  11. 求离散马尔科夫链的平稳分布+按照一定概率分布生成想要的样本
  12. 函数指针指向类的静态成员函数
  13. 基于Nokia S60的游戏开发之二
  14. 面向对象课程 - 寒假第三次作业 - C++计算器项目初始部分
  15. Haproxy+keepalived高可用集群实战
  16. cad抛物线曲线lisp_AutoCAD上精确实现抛物线和双曲线
  17. latex编译pdf winedt_XeLaTeX及WinEdt6.0入门指南资料.pdf
  18. 【Unity好用插件】PSD文件转UI插件——Psd 2 Unity uGUI Pro ★★★完整过程
  19. 操作系统学习笔记(五)---进程同步
  20. 画以载道:艺术演变的动力与社会思潮的嬗变

热门文章

  1. 手动将web项目的class文件打成jar包,手动打jar包,java -cvf,IDE打包底层指令
  2. 2022-2028年中国钢化玻璃行业市场研究及前瞻分析报告
  3. 2022-2028年中国节能建材行业深度调研及投资前景预测报告
  4. 浅显易懂 Makefile 入门 (01)— 什么是Makefile、为什么要用Makefile、Makefile规则、Makefile流程如何实现增量编译
  5. Git 常用操作(4)- 更改提交
  6. Docker学习(四)-----Docker容器常用命令
  7. 谷歌BERT预训练源码解析(二):模型构建
  8. 梯度优化算法Adam
  9. Gitea——私有git服务器搭建详细教程
  10. ALD对照CVD淀积技术的优势