python使用xlrd库处理excel__API常见基本操作
生活随笔
收集整理的這篇文章主要介紹了
python使用xlrd库处理excel__API常见基本操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- xlrd常見方法
- 字典、列表、集合常見操作
- 數(shù)據(jù)可視化常見操作
- 獲取Excel中的一列(評(píng)分),統(tǒng)計(jì)評(píng)分及其出現(xiàn)的次數(shù)
- 獲取Excel中的兩列(具體消費(fèi)項(xiàng)目,消費(fèi)金額)
xlrd常見方法
讀取一個(gè)sheet
workBook = xlrd.open_workbook('D:\\PycharmProjects\\flaskProject1\\templates\\xls\\豆瓣電影Top250.xls');sheet1 = workBook.sheets()[0] # 獲得第1張sheet,索引從0開始獲得名稱 、列數(shù)、行數(shù)
sheet2_name = sheet2.name # 獲得名稱sheet2_cols = sheet2.ncols # 獲得列數(shù) sheet2_nrows = sheet2.nrows # 獲得行數(shù)獲取一行、一列
sheet2_nrows4 = sheet2.row_values(4) # 獲得第4行數(shù)據(jù)sheet2_cols2 = sheet2.col_values(2) # 獲得第2列數(shù)據(jù)cell23 = sheet2.row(2)[5].value # 查看第3行第6列數(shù)據(jù)字典、列表、集合常見操作
兩個(gè)列表合并為字典
keys = ['a', 'b', 'c','a'] values = [1, 2, 3,9] dictionary = dict(zip(keys, values)) print(dictionary)數(shù)據(jù)可視化常見操作
獲取Excel中的一列(評(píng)分),統(tǒng)計(jì)評(píng)分及其出現(xiàn)的次數(shù)
workBook = xlrd.open_workbook('D:\\PycharmProjects\\flaskProject1\\templates\\xls\\豆瓣電影Top250.xls');sheet1 = workBook.sheets()[0] # 獲得第1張sheet,索引從0開始score1 = sheet1.col_values(4) # 獲取評(píng)分信息(Excel中從0開始數(shù),第四列)score2 = Counter(score1) # 評(píng)分及其次數(shù)的鍵值對(duì) score3 = sorted(score2.items(), key=lambda dict1: score2[0]) # 排序后的 for item in score3:score.append(item[0])num.append(item[1])獲取Excel中的兩列(具體消費(fèi)項(xiàng)目,消費(fèi)金額)
workBook2 = xlrd.open_workbook('D:\\ProgramFiles\\docTest\excel\\TeamSettlementDetails.xls') sheet2 = workBook2.sheets()[0]aa=Counter(sheet2.col_values(4))print(aa) # Counter({'other': 7862, 'catering': 2605, 'ticket': 2486, 'hotel': 1343, 'meeting': 979, 'training': 617, 'guid': 407, 'party': 84})moduleName=sorted(set(aa)) # 獲得具體消費(fèi)項(xiàng)目種類print(moduleName) # ['catering', 'guid', 'hotel', 'meeting', 'other', 'party', 'ticket', 'training']print(moduleName[0]) # cateringsheet2_nrows = sheet2.nrows # 獲得行數(shù) for i in range(sheet2_nrows): # 逐行打印sheet2數(shù)據(jù)if sheet2.row_values(i)[4] == 'catering': # catering的消費(fèi)金額# print(sheet2.row_values(i)[6])cateringTotal += sheet2.row_values(i)[6]if sheet2.row_values(i)[4] == 'guid': # guid的消費(fèi)金額# print(sheet2.row_values(i)[6])guidTotal += sheet2.row_values(i)[6]if sheet2.row_values(i)[4] == 'ticket':# print(sheet2.row_values(i)[6]) 與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python使用xlrd库处理excel__API常见基本操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python+xlrd+echarts数
- 下一篇: 怎样才算精通Python?