免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Python 進階(六): Excel 基本操作

目錄

1. 概述

在現(xiàn)實中,很多工作都需要與數(shù)據(jù)打交道,Excel 作為常用的數(shù)據(jù)處理工具,一直備受人們的青睞,而大部分人都是手動操作 Excel,如果數(shù)據(jù)量較小且是一些簡單的操作還好說,但如果數(shù)據(jù)量較大或是一些復雜的操作,工作量可想而知,因此,我們需要掌握一種簡單、高效的方法來操作 Excel。

在數(shù)據(jù)處理方面,Python 一直扮演著重要的角色,對于 Excel 操作,它有著完整且成熟的第三方庫,使用也較為簡單。

Python 中常用 Excel 操作庫如下:

  • xlrd:從 Excel 中讀取數(shù)據(jù),支持 xls、xlsx。

  • xlwt:向 Excel 中寫入數(shù)據(jù),支持 xls。

  • xlutils:提供了一些 Excel 的實用操作,比如復制、拆分、過濾等,通常與 xlrd、xlwt 一起使用。

  • XlsxWriter:向 Excel 中寫入數(shù)據(jù),支持 xlsx。

  • openpyxl :用于讀寫 Excel,支持 xlsx。

2. 寫入

我們向 Excel 中寫入一些數(shù)據(jù)。

2.1 使用 xlwt

通過 pip install xlwt 命令安裝。

import xlwt# 創(chuàng)建工作簿wb = xlwt.Workbook()# 創(chuàng)建表單sh = wb.add_sheet('test')# 創(chuàng)建字體對象font = xlwt.Font()# 字體加粗font.bold = Truealm = xlwt.Alignment()# 設置左對齊alm.horz = 0x01# 創(chuàng)建樣式對象style1 = xlwt.XFStyle()style2 = xlwt.XFStyle()style1.font = fontstyle2.alignment = alm# write 方法參數(shù)1:行,參數(shù)2:列,參數(shù)3:內(nèi)容sh.write(0, 1, '姓名', style1)sh.write(0, 2, '年齡', style1)sh.write(1, 1, '張三')sh.write(1, 2, 50, style2)sh.write(2, 1, '李四')sh.write(2, 2, 30, style2)sh.write(3, 1, '王五')sh.write(3, 2, 40, style2)sh.write(4, 1, '趙六')sh.write(4, 2, 60, style2)sh.write(5, 0, '平均年齡', style1)# 保存wb.save('test.xls')

執(zhí)行結(jié)果:

2.2 使用 XlsxWriter

通過 pip install XlsxWriter 命令安裝。

import xlsxwriter# 創(chuàng)建工作簿workbook = xlsxwriter.Workbook('test.xlsx')# 創(chuàng)建表單sh = workbook.add_worksheet('test')fmt1 = workbook.add_format()fmt2 = workbook.add_format()# 字體加粗fmt1.set_bold(True)# 設置左對齊fmt2.set_align('left')# 數(shù)據(jù)data = [    ['', '姓名', '年齡'],    ['', '張三', 50],    ['', '李四', 30],    ['', '王五', 40],    ['', '趙六', 60],    ['平均年齡', '', ]]sh.write_row('A1', data[0], fmt1)sh.write_row('A2', data[1], fmt2)sh.write_row('A3', data[2], fmt2)sh.write_row('A4', data[3], fmt2)sh.write_row('A5', data[4], fmt2)sh.write_row('A6', data[5], fmt1)chart = workbook.add_chart({'type': 'line'})workbook.close()

XlsxWriter 可以很方便的生成圖表。

import xlsxwriter# 創(chuàng)建工作簿wk = xlsxwriter.Workbook('test.xlsx')# 創(chuàng)建表單sh = wk.add_worksheet('test')fmt1 = wk.add_format()fmt2 = wk.add_format()# 字體加粗fmt1.set_bold(True)# 設置左對齊fmt2.set_align('left')# 數(shù)據(jù)data = [    ['', '姓名', '年齡'],    ['', '張三', 50],    ['', '李四', 30],    ['', '王五', 40],    ['', '趙六', 60],    ['平均年齡', '', ]]sh.write_row('A1', data[0], fmt1)sh.write_row('A2', data[1], fmt2)sh.write_row('A3', data[2], fmt2)sh.write_row('A4', data[3], fmt2)sh.write_row('A5', data[4], fmt2)sh.write_row('A6', data[5], fmt1)'''area:面積圖bar:直方圖column:柱狀圖line:折線圖pie:餅圖doughnut:環(huán)形圖radar:雷達圖'''chart = wk.add_chart({'type': 'line'})# 創(chuàng)建圖表chart.add_series(    {        'name':'=test!$B$1',        'categories':'=test!$B$2:$B$5',        'values':   '=test!$C$2:$C$5'    })chart.set_title({'name':'用戶年齡折線圖'})chart.set_x_axis({'name':'姓名'})chart.set_y_axis({'name':'年齡'})sh.insert_chart('A9', chart)wk.close()

執(zhí)行結(jié)果:

3. 讀取

我們使用 xlrd 讀取之前寫入的數(shù)據(jù),使用 pip install xlrd 命令安裝。

import xlrd# 打開wb = xlrd.open_workbook('test.xlsx')print( 'sheet名稱:', wb.sheet_names())print( 'sheet數(shù)量:', wb.nsheets)# 根據(jù) sheet 索引獲取 sheetsh = wb.sheet_by_index(0)# 根據(jù) sheet 名稱獲取 sheet# sh = wb.sheet_by_name('test')print( u'sheet %s 有 %d 行' % (sh.name, sh.nrows))print( u'sheet %s 有 %d 列' % (sh.name, sh.ncols))print('第二行內(nèi)容:', sh.row_values(1))print('第三列內(nèi)容:', sh.col_values(2))print('第二行第三列的值為:', sh.cell_value(1, 2))print('第二行第三列值的類型為:', type(sh.cell_value(1, 2)))

4. 修改

之前寫入的數(shù)據(jù)還有一個平均年齡是空著的,我們先讀取之前寫入的數(shù)據(jù),再計算出平均值,最后將平均值寫入。這里要用到 xlutils 模塊,使用 pip install xlutils 安裝。

import xlrd, xlwtfrom xlutils.copy import copydef avg(list):    sumv = 0    for i in range(len(list)):        sumv += list[i]    return int(sumv / len(list))# formatting_info 為 True 表示保留原格式wb = xlrd.open_workbook('test.xls', formatting_info=True)# 復制wbc = copy(wb)sh = wb.sheet_by_index(0)age_list = sh.col_values(2)age_list = age_list[1:len(age_list)-1]avg_age = avg(age_list)sh = wbc.get_sheet(0)# 設置左對齊alm = xlwt.Alignment()alm.horz = 0x01style = xlwt.XFStyle()style.alignment = almsh.write(5, 2, avg_age, style)wbc.save('test.xls')

執(zhí)行結(jié)果:


本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
史上最全Python 操作 Excel庫總結(jié)!
Python操作Excel 模塊,你猜哪家強?
python處理excel總結(jié)
使用python操作Excel——xlrd、xlwt、xlutils庫
針對不同場景的Python合并多個Excel方法
普通小白學會Python到底具體能做什么呢?
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服