txt 文件
1,打开文件
open(path,flag[,encoding[errors])
path:打开文件的路径
flag:打开方式
r —— 只读方式,文件的描述符放在文件开头
rb — 以二进制格式打开一个文件,用于只读,文件的描述符放在文件开头
r+ — 打开一个文件用于读写,文件的描述符放在文件开头
w — 打开一个文件只用于写入,如果该文件存在会覆盖,如果不存在则创建新文件
wb — 打开一个文件只用于写入二进制,如果该文件已经存在会覆盖,如果不存在则创建新文件
w+ — 打开一个文件用于读写
a — 打开一个文件用于追加,如果文件存在,
文件描述符将会放到文件末尾
encoding:编码格式
errors:错误处理
txt文件读取
打开文件
path = r"../aa.txt"
#ignore 忽略错误
f=open(path,'r',encoding='utf-8',errors='ignoer')
1.读文件内容
#读取文件全部内容
str1=f.read()
print(str1)
2读取指定字符数
str2=f.read(9)
print("*"+str2+"*")
3.读取整行,包括“\n”符
str4=f.readline()
print(str4)
str5=f.readline()
print(str5)
4.读取指定字符数
str11=f.readline(3)
print(str11)
str22=f.readline(6)
print(str22)
5.读取所有行并返回列表
list1=f.readlines()
print(list1)
6.若给定的数字大于0,返回实际size字节的行数
list2=f.readlines(7)
print(list2)
7.修改描述符的位置(删除)
f.seek(2)
list3=f.read()
print(list3)
一个文件的完整操作过程
try:
fl=open(path,'r',encoding='utf-8')
print(fl.read())
finally:
if fl:
fl.close()
#
with open(path,'r',encoding='utf-8') as f2:
print(f2.read())
# a=f2.read()
# print(type(a))
txt文件写入
path = r"../cc.txt"
#ignore 忽略错误
f=open(path,'a')
#写文件
#1,将信息写入缓冲区
f.write('11444444441')
#2,刷新缓冲区
#直接把内部缓冲区的数据写入文件,而不是被动等待自动刷新缓冲区写入
f.flush()
#循环写入
while 1:
f.write("python\n")
time.sleep(0.01)
f.close()#关闭文件
with open(path,'a') as f2:
f2.write("2abcdefg")
文件编码
with open(path,'wb') as ff:
str='python is a good'
ff.write(str.encode('utf-8'))
文件解码
with open(path,'rb') as fa:
data=fa.read()
newdata=data.decode('utf-8') #解码类型
csv文件
csv文件读取
import csv
def readCsv(path):
infolist=[]
with open(path,'r') as f :
allFile=csv.reader(f)
# print(allFile)
for row in allFile:
infolist.append(row)
print(row)
return infolist
path=r'C:\Users\xiaofeng\PycharmProjects\基础\13读取csv文件\学号1.csv'
readCsv(path)
csv文件写入
import csv
def writeCsv(path, data):
with open(path, 'w') as f:
writer = csv.writer(f)
for rowData in data:
writer.writerow(rowData)
f.close()
path =r'C:\Users\xiaofeng\PycharmProjects\基础\13读写csv文件\学号1.csv'
writeCsv(path, ([['1', '1', '1'], [4, 5, 6], [7, 8, 9]]))
list—tuple—dict—set操作
写入
list1=[1,2,3,4,'aa','cc']
path=r'.\1.txt'
f=open(path,'wb') #二进制写入
#写入
pickle.dump(list1,f)
f.close()
读取
f1=open(path,'rb')
list2=pickle.load(f1)
print(list2)
f1.close()
pdf 文件操作
import sys
import importlib
importlib.reload(sys)
from pdfminer.pdfparser import PDFParser,PDFDocument
from pdfminer.pdfinterp import PDFResourceManager,PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LTTextBoxHorizontal,LAParams
from pdfminer.pdfinterp import PDFTextExtractionNotAllowed
def readPDF(path,toPath):
#以二进制形式打开pdf文件
f=open(path,'rb')
# 创建一个破地方文档分析器
parser=PDFParser(f)
#创建pdf文档
pdfFile=PDFDocument()
#链接创建的文档和分析器 双向链接
parser.set_document(pdfFile)
pdfFile.set_parser(parser)
#提供初始化密码
pdfFile.initialize()
#检测文档是否提供txt转换
if not pdfFile.is_extractable:
raise PDFTextExtractionNotAllowed
else:
#解析数据
#数据管理器
manager=PDFResourceManager()
#创建一个pdf设备对象
laparams=LAParams()
device=PDFPageAggregator(manager,laparams)
# 解释器对象
interpretr=PDFPageInterpreter(manager,device)
# 开始循环处理 每次处理一页
print(pdfFile.get_pages())
for page in pdfFile.get_pages():
print(page)
interpretr.process_page(page)#解释page这一页
layout=device.get_result()
for x in layout:
if (isinstance(x,LTTextBoxHorizontal)):
with open(toPath,'a') as f:
str=x.get_text()
print(str)
f.write(str+'\n')
topath=r'C:\Users\xiaofeng\PycharmProjects\基础\13读写csv文件\2.txt'
path=r'C:\Users\xiaofeng\PycharmProjects\基础\13读写csv文件\qqqq.pdf'
readPDF(path,topath)
文章末尾
xiaofengfeng的博客
你要坚持努力而不是你努力坚持——xiaofengfeng