一、txt文件
读取:
方式一:
import codecs
with codecs.open('\,txt','r','utf-8') as f:
text=f.read() #text为str类型
方式二:
words=[line.strip() for line in open(filepath,mode='r',encoding='utf-8').readlines()] #words为list类型
def readtxt(filepath,encoding='gbk'):
words=[]
f=open(filepath,encoding=encoding)
lines=f.readlines()
for line in lines:
if line.isspace():
continue
else:
words.append([line])
return words
def readtxt(filepath,encoding='gbk'): #这种读法返回的是列表
words=[line.strip() for line in open(filepath,mode='r',encoding=encoding).readlines()]
return words
def readtxt2(filepath,encoding='utf-8'): #这种读法返回的是[()]
tuples=[]
for t in open(filepath).read().split():
a,b=t.strip('()').split(',')
tuples.append((a,b))
return tuples
def readtxt3(filepath,encoding='utf-8'):
words=np.loadtxt(filepath,delimiter=' ')
return words
保存:
f=open('filepath','w',encoding='utf-8')
f.writeline([])
f.close
二、csv文件
读取:
方式一:利用numpy读取
import numpy as np
data=np.loadtxt(open('路径.csv','rb'),delimiter=",",skiprows=n,usecols=[2,3])
方式二:利用pandas
import pandas as pd
data=pd.read_csv(r'.csv',sep=',',header='infer')
保存:
方式一:
file=open('test.csv','a+')
file.write('')
file.close()
方式二:(对DataFrame类型数据)
df1=pd.DataFrame(data=img_label_list,columns=['filepath','label'])
df1.to_csv('.csv',index=False)
三、xlsx、xls文件
读取
方式一:pandas库
import pandas as pd
df1=pd.read_excel('.xlsx') #df1为DataFrame格式
方式二:
import xlrd
booksheet=xlrd.open_workbook('.xls')
print(workbook.sheet_names()) #查看所有sheet
booksheet=workbook.sheet_by_index(0)
方式三:
from openpyxl import load_workbook
workbook = load_workbook('D:\\Py_exercise\\test_openpyxl.xlsx')
保存:
from openpyxl import Workbook
workbook = Workbook()
booksheet = workbook.active #获取当前活跃的sheet,默认是第一个sheet
#存第一行单元格cell(1,1)
booksheet.cell(1,1).value = 6 #这个方法索引从1开始
booksheet.cell("B1").value = 7
#存一行数据
booksheet.append([11,87])
workbook.save("test_openpyxl.xlsx")