需求:
把app_student.xls里面的数据,
1、如果这一行数据里面有乱码(及包含?),那么就删掉
2、再加上一列,是否毕业
3、如果班级是天蝎座的话,毕业这一列写成毕业
4、其他班级的写成未毕业
原始数据:
实现:
import xlrd,xlwt EXCEL_NAME = ‘app_student.xls‘ def delete_messy_code(excel_name): #删除乱码 book = xlrd.open_workbook(excel_name) sheet = book.sheet_by_index(0) data = [] for i in range(sheet.nrows): if ‘?‘ in str(sheet.row_values(i)):#仅将没有乱码的数据加入data[] continue else: data.append(sheet.row_values(i)) return data def update_col(data): #增加列,并填入是否毕业 for d in data: if d[5] == ‘grade‘: d.append(‘是否毕业‘) elif d[5] == ‘天蝎座‘: d.append(‘毕业‘) else: d.append(‘未毕业‘) return data def wt_excel(excel_name): #将处理后的结果写入Excel rb = xlwt.Workbook() rbs =rb.add_sheet(‘sheet1‘) data = delete_messy_code(excel_name) row = 0 for field in update_col(data): for col, f in enumerate(field): rbs.write(row,col,f) row += 1 rb.save(excel_name) wt_excel(EXCEL_NAME)
原文地址:https://www.cnblogs.com/dongrui624/p/9054085.html
时间: 2024-10-08 22:50:51