目录
一、 自带open处理函数
1. 读取文本文件
with open('test.txt') as file:
conts=file.read() #一次性全部读
conts1=conts.rstrip() #删除末尾的空行,strip可以删除左边的空行
print(conts)
with open('file\test.txt') as file:
for myline in file: #用for遍历文件中的每一行
print myline.rstrip() #一行一行输出
#将文件内容存储在列表中,方便处理
with open('file\test.txt') as file:
mylines=file.readlines() #line是一个列表,存储了文件的每一行
for line in mylines
print mylines.rstrip()
2. 写文本文件
默认是读取文件r,w写入模式时,当文件不存在将自动创建文件。
filename='test.txt'
with open(filename,'w') as fwt:
fwt.write('I love you\n')
追加文件a并不会覆盖原文件
filename='test.txt'
with open(filename,'a') as fwt:
fwt.write('I love you,too.\n')
3.处理文件异常try-except
略
二、 xlrd和xlwt对工作簿文件的读取和写入
import xlrd2 as rd
import xlwt as wt
#############################################################################
#####读######################################################################
#############################################################################
wb=rd.open_workbook(r'te.xlsx')
#sheet
sheet=wb.sheets()
#获取某一sheet的两种方法
sheet0=wb.sheet_by_name('Sheet1')
sheet1=wb.sheets()[1]
#获取行数列数
print(sheet1.nrows,sheet1.ncols,sheet0.ncols,sheet0.nrows)
#名字
sheetsname=wb.sheet_names()
sheetname1=sheetsname[1]
print(sheetsname,sheetname1,sheet1)
#获取行或列的值
row_value=sheet0.row_values(2)
col_value=sheet1.col_values(2)
value=sheet0.cell(0,0).ctype
value1=sheet0.cell(0,0).value.encode('utf-8')
value2=sheet0.cell_value(0,0)#单元格类型
#某一行内容,某一列内容,单元格
print(row_value,col_value,value,value2)
#遍历输出表
for i in range(sheet0.nrows):
print('第%s行为%s'%(i,sheet0.row_values(i)))
######AN按列输出同理
#或
for m in range(sheet1.nrows):
for n in range(sheet1.ncols):
print(sheet1.cell_value(m,n))
#########################################################################
#####写##################################################################
#########################################################################
workbook=wt.Workbook()#建立一个对象,W大写
sheet1=workbook.add_sheet('表1',cell_overwrite_ok=True)
sheet1.write(0,0,'类型')
sheet1.write(0,1,'华为')
sheet1.write(0,2,'小米')
workbook.save('xianjian2.xls')
print('创建excel文件成功')
三、openpyxl对EXCEL文件的处理
1. 读取
from openpyxl import load_workbook
#from openpyxl import Worksheet
wb = load_workbook(filename = 'empty_book.xlsx')
sheet_ranges = wb['range names']
print(sheet_ranges['D18'].value)
sheet=wb['data']
#sheet:Worksheet=wb['data'] #还不懂':'是啥意思
#遍历
for vl in sheet:
print vl
2.写入
from openpyxl import Workbook
wb=Workbook()
ws1 = wb.active
ws1.title = "range names"
_ = ws3.cell(column=15, row=16, value="{0}".format(get_column_letter(15)))
for row in range(1, 40):
ws1.append(range(600))
sheet=wb.create_sheet('data')
sheet['A1']=20
sheet.append(['hhh','abc'])
for i in range(10):
sheet.append(['user{i}','123'])
wb.save('myo.xlsx')
四、自带csv库
1. 写入
import csv
with open('./data.csv',mode='w',newline=' ',encoding='utf-8') as file:
fw=csv.write(file)
fw.writerow(['abdcv','rr'])
fw.writerow(['ad','wr'])
#等效于
fw.writerows([('abdcv','rr'),('ad','wr')])
2. 读取
import csv
with open('./data.csv',newline=' ',encoding='utf-8') as file:
fr=csv.reader(file)
for line in fr:
print(line)
五、json
略
六、pandas
pandas支持大部分的主流文件格式进行数据读写,常用格式及接口为:
-
文本文件,主要包括csv和txt两种等,相应接口为read_csv()和to_csv(),分别用于读写数据
-
Excel文件,包括xls和xlsx两种格式均得到支持,底层是调用了xlwt和xlrd进行excel文件操作,相应接口为read_excel()和to_excel()
-
SQL文件,支持大部分主流关系型数据库,例如MySQL,需要相应的数据库模块支持,相应接口为read_sql()和to_sql()
此外,pandas还支持html、json等文件格式的读写操作。