网站首页 > 技术文章 正文
Python提供了用于创建,写入和读取文件的内置函数。 可以在python中处理的文件有两种类型,普通文本文件和二进制文件(二进制0和1)。 在本小节中,我们将研究从文件逐行读取的问题。
使用readlines()
readlines()用于一次读取所有行,然后将它们作为列表中的字符串元素的每一行返回。 此功能可用于小型文件,因为它将整个文件内容读取到内存中,然后将其拆分为单独的行。 我们可以遍历列表,并使用strip()函数删除换行符'\ n'。
举例说明:
L = ["Geeks\n", "for\n", "Geeks\n"] #换行符'\ n'
file1 = open('myfile.txt', 'w') #创建一个txt文档,以w方式写入,w代表写入write
file1.writelines(L) #全部写入
file1.close()
file1 = open('myfile.txt', 'r') #读取txt文档里面的内容,r代表读取read
Lines = file1.readlines() #全部读取出来
count = 0
for line in Lines: #使用for循环遍历出来
print(line.strip())
print("Line{}: {}".format(count, line.strip()))
输出:
Line1: Geeks
Line2: for
Line3: Geeks
使用readline()
readline()函数读取文件的一行并以字符串形式返回。 它使用参数n,该参数指定将读取的最大字节数。 但是,即使n超过了行的长度,也不会读取多于一行。 读取大文件时,此方法将非常有效,因为它不会一次读取所有数据,而是逐行读取。 readline()返回文件的下一行,该行的末尾包含换行符。 同样,如果到达文件末尾,它将返回一个空字符串。
代码演示:
L = ["Geeks\n", "for\n", "Geeks\n"]
file1 = open('myfile.txt', 'w')
file1.writelines((L))
file1.close()
file1 = open('myfile.txt', 'r')
count = 0
while True:
count += 1
line = file1.readline()
if not line:
break
print("Line{}: {}".format(count, line.strip()))
file1.close()
输出:
Line1 Geeks
Line2 for
Line3 Geeks
使用for循环
L = ["Geeks\n", "for\n", "Geeks\n"]
file1 = open('myfile.txt', 'w')
file1.writelines(L)
file1.close()
file1 = open('myfile.txt', 'r')
count = 0
print("Using for loop")
for line in file1:
count += 1
print("Line{}: {}".format(count, line.strip()))
file1.close()
输出:
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
With 语句:
代码演示:
L = ["Geeks\n", "for\n", "Geeks\n"]
with open("myfile.txt", "w") as fp:
fp.writelines(L)
count = 0
print("Using readlines()")
with open("myfile.txt") as fp:
Lines = fp.readlines()
for line in Lines:
count += 1
print("Line{}: {}".format(count, line.strip()))
count = 0
print("\nUsing readline()")
with open("myfile.txt") as fp:
while True:
count += 1
line = fp.readline()
if not line:
break
print("Line{}: {}".format(count, line.strip()))
count = 0
print("\nUsing for loop")
with open("myfile.txt") as fp:
for line in fp:
count += 1
print("Line{}: {}".format(count, line.strip()))
输出:
Using readlines()
Line1: Geeks
Line2: for
Line3: Geeks
Using readline()
Line1: Geeks
Line2: for
Line3: Geeks
Using for loop
Line1: Geeks
Line2: for
Line3: Geeks
祝学习愉快!
- 上一篇: python中读取图片的6种方式
- 下一篇: python 利用python读取DOC文件
猜你喜欢
- 2025-01-06 3分钟教会你用Python读取MySQL中的数据
- 2025-01-06 Python高效管理JSON文件:读写、更新、删除全攻略
- 2025-01-06 读取txt、doc、docx、pdf文件——python
- 2025-01-06 20 天学 Python 文件操作:Day 2 深入理解文件读取方法
- 2025-01-06 python 利用python读取DOC文件
- 2025-01-06 python中读取图片的6种方式
- 2025-01-06 Python读写docx文件
- 2025-01-06 Python 文件操作全指南:从读写到高级操作
- 2025-01-06 python读取文件
- 2025-01-06 Python如何读写xml
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)