1. 程式人生 > >小練習-excel刪除亂碼?加一列

小練習-excel刪除亂碼?加一列

title line 表頭 day lse end ont 新建 pen

import xlrd,xlwt,os
book1=xlrd.open_workbook(‘E:/learn/python/day7/z_api/data/app_student.xls‘)
book2=xlwt.Workbook()#新建一個excel
sheet=book1.sheet_by_index(0)#打開sheet頁
sheet2=book2.add_sheet(‘sheet1‘)#添假一個sheet頁
title=sheet.row_values(0)#獲取表頭
title.append(‘是否畢業‘)
print(title)
tmp=1
for index,t in enumerate(title):
sheet2.write(0,index,t)
for row in range(1,sheet.nrows):#從第一行循環獲取每行數據
line=sheet.row_values(row)#獲取每一行數據
if ‘?‘ in str(line):#判斷亂碼是否在每一行
continue
else:
if line[5]==‘天蠍座‘:
line.append(‘已畢業‘)
else:
line.append(‘未畢業‘)
for index,col in enumerate(line):
sheet2.write(tmp,index,col)
tmp+=1
os.remove(‘E:/learn/python/day7/z_api/data/app_student.xls‘)
book2.save(‘E:/learn/python/day7/z_api/data/app_student.xls‘)

小練習-excel刪除亂碼?加一列