Python讀寫excel練習_去除excel中亂碼行,並添加列

來源:互聯網
上載者:User

標籤:update   values   name   span   work   write   其他   刪除   val   

需求:

把app_student.xls裡面的資料,
1、如果這一行資料裡面有亂碼(及包含?),那麼就刪掉
2、再加上一列,是否畢業
3、如果班級是天蠍座的話,畢業這一列寫成畢業
4、其他班級的寫成未畢業

未經處理資料:

實現:
import xlrd,xlwtEXCEL_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 datadef update_col(data): #增加列,並填入是否畢業    for d in data:        if d[5] == ‘grade‘:            d.append(‘是否畢業‘)        elif d[5] == ‘天蠍座‘:            d.append(‘畢業‘)        else:            d.append(‘未畢業‘)    return datadef 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)

 

Python讀寫excel練習_去除excel中亂碼行,並添加列

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.