一、读写csv数据
使用with..as可以不用对文件关闭操作
import csv '''读csv数据''' with open('csvName.csv', 'rb') as rf: reader = csv.reader(rf) '''写csv数据''' d = ['Id', 'Name', 'Sex', 'age', 'score'] with open('csvName1.csv', 'wb') as wf: writer = csv.writer(wf) writer.writerow(d)
二、读写excel文件
'''读excel''' import xlrd, xlwt book = xlrd.open_workbook('sxlName.xlsx') sheet = book.sheet_by_index(0) print sheet.nrows, sheet.ncols #行数,列数 cell = sheet.cell(0, 0) #一个单元格的对象 value = cell.value row = sheet.row(1) #一列的对象 rowValues = sheet.row_values(1) #一列数据 clo = sheet.col(1) #一行对象 cloValues = sheet.col_values(1) #一行数据 sheet.put_cell() #添加一个单元格 '''写excel''' wbook = xlwt.Workbook() wsheet = wbook.add_sheet('sheetNmae') wsheet.write()