python中f.read()与f.readline区别 | 您所在的位置:网站首页 › Python中fd函数 › python中f.read()与f.readline区别 |
众所周知在python中读取文件常用的三种方法:read(),readline(),readlines(),今天看项目是又忘记他们的区别了。以前看书的时候觉得这东西很简单,一眼扫过,待到用时却也只知道有这么几个方法,不懂得它的原理与用法。也许吧,没有永远的记忆,况且根本没有用心去记它。话不多说,来一起看看详细的介绍: 假设a.txt 一、read([size])方法 read([size])方法从文件当前位置起读取size个字节,若无参数size,则表示读取至文件结束为止,它范围为字符串对象 f = open("a.txt") lines = f.read() print lines print(type(lines)) f.close()输出结果: Hello Welcome What is the fuck... #字符串类型二、readline()方法 从字面意思可以看出,该方法每次读出一行内容,所以,读取时占用内存小,比较适合大文件,该方法返回一个字符串对象。 1 2 3 4 5 6 7 f = open("a.txt") line = f.readline() print(type(line)) while line: print line, line = f.readline() f.close() 输出结果: Hello Welcome What is the fuck...三、readlines()方法读取整个文件所有行,保存在一个列表(list)变量中,每行作为一个元素,但读取大文件会比较占内存 f = open("a.txt") lines = f.readlines() print(type(lines)) for line in lines: print line, f.close()输出结果: 1 2 Hello 3 Welcome 4 What is the fuck...四、linecache模块 当然,有特殊需求还可以用linecache模块,比如你要输出某个文件的第n行: 1 2 3 # 输出第2行 text = linecache.getline(‘a.txt',2) print text, python 读写文件类型主要有txt, csv,excel, sas,spss 和mysql, 在这里主要介绍用的最多的txt和csv文件
读写 方式主要分为三种: 1.python 自带的函数 read,readline ,readlines,write 2.pandas模块中的read_csv,to_csv 3.csv模块中的reader,writer 下面以txt为例进行介绍,csv文件的读写基本与txt一致,把文件名换了即可
txt原内容 learn python learn pandas numpy
一、python 内置函数 1.先介绍open函数 open(name[, mode[, buffering]]) name : '文件名称'。 mode : 打开模式,默认为只读(r),可以选择为写(w) 常用的: r 只读 默认格式 rb 二进制打开文件用于只读 r+ 打开文件用于读写 w 只写 wb 二进制打开文件用于只写 w+ 打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。 如果该文件不存在,创建新文件 buffering : 如果 buffering 的值被设为 0,就不会有寄存。如果 buffering 的值取 1,访问文件时会寄存 注意: 很多时候会出现以下两种情况 采用with.. as 多是为了保证程序的健壮性,防止出现IO异常,导致文件打不加 推荐使用with.. as 格式 file = open('.//data//lesson1//1.txt') file.close() with open('.//data//lesson1//1.txt', 'r') as file: print(file.read()) 2.read #读取txt文本 #方法1: read() read()是直接读取txt中所有内容,返回字符串 file = open('.//data//lesson1//1.txt') lines = file.read() print(lines) print(type(lines)) file.close() #记得把文件关闭 输出为: learn python learn pandas numpy 3.readline # 方法2: readline 一行一行读取文件,返回的是字符串,仅当没有足够内存可以一次读取整个文件时,才应该使用.readline() file = open('.//data//lesson1//1.txt') #1.手动一行一行读 line1 = file.readline() print(line1) line2 = file.readline() print(line2) file.close() 输出为: learn python learn pandas numpy #方法2: readline 方法用于从文件读取整行,包括 "\n" 字符 file = open('.//data//lesson1//1.txt') #2.连续读 line = file.readline() while line: print(line) line = file.readline() file.close() 输出为: learn python learn pandas numpy #注意:readline(num) 表示读取该行中前num个字母,包含空格 file = open('.//data//lesson1//1.txt') line = file.readline(10) print(line) file.close() 输出为: learn pyth 4.readlines #方法3 readlines 读取整个文件所有行,保存在一个列表(list)变量中,每次读取一行 file = open('.//data//lesson1//1.txt') lines = file.readlines() print(lines) #输出所有内容 result = [] for line in lines: print(line) #一行一行输出所有内容 result.append(line) file.close() 输出为: ['learn python\n', 'learn pandas numpy '] learn python learn pandas numpy 5.write 写文件 #在2.txt中写入 result的结果,2.txt不存在可以自己创建 open('.//data//lesson1//2.txt','w').write('%s' % '\n'.join(result)) 二、pandas模块中的read_csv(),to_csv() #read_csv 输出为DataFrame, DataFrame后面会具体讲解 import pandas as pd file_csv = pd.read_csv('.//data//lesson1.txt') print(type(file_csv)) 输出为: #to_csv 写文件 import pandas as pd file_csv = pd.to_csv('.//data//lesson1.txt',result) #result是写入的内容 三、csv模块中的reader,writer #csv中的reader 读文件 import csv file = './/data//lesson1//1.txt' data = [] with open(file,'r') as f: result = csv.reader(f) for line in result: data.append(line) print(data) 输出: [['learn python'], ['learn pandas numpy ']] #csv中的writer 写文件,没有该文件程序会自己创建 with open('.//data//lesson1//3.txt','w') as f: writer = csv.writer(f) writer.writerows(data)
|
CopyRight 2018-2019 实验室设备网 版权所有 |