python3:excel操作之讀取數(shù)據(jù)并返回字典 + 寫入的案例
excel寫入數(shù)據(jù),使用openpyxl庫
class WriteExcel:
def __init__(self,path):
self.path = path
def write_excel(self, sheet_name, content):
"""
在excel指定sheet中的寫入指定內(nèi)容,以追加方式
:return:
"""
wb = openpyxl.load_workbook(self.path)
ws = wb[sheet_name]
# 獲取最大行
row_num = ws.max_row
try:
ws.cell(row=row_num+1, column=1).value = content
except Exception as msg:
print('寫入excel失敗:', msg)
finally:
wb.save(self.path)
if __name__ == '__main__':
WE = WriteExcel('../config/data.xlsx')
WE.write_excel(sheet_name='user', content='瑟瑟發(fā)抖')

excel讀取數(shù)據(jù),使用xlrd庫
class ReadExcel:
def __init__(self,path):
self.path = path
def read_excel(self,row):
"""
遍歷excel所有sheet,并以字典返回
:param row:
:return:
"""
with xlrd.open_workbook(self.path, 'rb') as book:
sheets = book.sheet_names() # 找到所有sheets
data_dict = {}
for sheet in sheets:
table = book.sheet_by_name(sheet) # 找到要操作的sheet
# 獲取sheet所有列數(shù)
col_num = table.ncols
# 讀取第一行的值,作為每個sheet返回字典的key
keys = table.row_values(0)
# 讀取除指定行,作為每個sheet返回字典的value
values = table.row_values(row)
# 遍歷所有列,并以字典接收,其中第一行作為字典的key,其他行作為字典的value
sheet_dict = {}
for col in range(col_num):
sheet_dict[keys[col]] = values[col]
# 遍歷所有sheet,并以字典接收返回,其中sheet名稱作為字典的key,每個sheet的數(shù)據(jù)作為字典的value
data_dict[sheet] = sheet_dict
return data_dict

讀取結(jié)果:

補(bǔ)充知識:Python+selenium+ddt數(shù)據(jù)驅(qū)動測試
我就廢話不多說了,大家還是直接看代碼吧~
import ddt
testData = ['1','2','3']
print testData
@ddt.ddt
class Bolg(unittest.TestCase):
def setUp(self):
print('setUp')
@ddt.data(*testData)
def test_l(self, data):
print(data)
def tearDown(self):
print('tearDown')
if __name__ == "__main__":
unittest.main()
============
1
2
3
以上這篇python3:excel操作之讀取數(shù)據(jù)并返回字典 + 寫入的案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Python數(shù)據(jù)分析之pandas讀取數(shù)據(jù)
- Python 循環(huán)讀取數(shù)據(jù)內(nèi)存不足的解決方案
- Python隨機(jī)函數(shù)random隨機(jī)獲取數(shù)字、字符串、列表等使用詳解
- python實現(xiàn)scrapy爬蟲每天定時抓取數(shù)據(jù)的示例代碼
- Python從文件中讀取數(shù)據(jù)的方法步驟
- python從PDF中提取數(shù)據(jù)的示例
- python從Oracle讀取數(shù)據(jù)生成圖表
- Python爬取數(shù)據(jù)并實現(xiàn)可視化代碼解析
- Python定時從Mysql提取數(shù)據(jù)存入Redis的實現(xiàn)
- 使用Python腳本從文件讀取數(shù)據(jù)代碼實例
- python3實現(xiàn)從kafka獲取數(shù)據(jù),并解析為json格式,寫入到mysql中
- Python實現(xiàn)一個自助取數(shù)查詢工具
相關(guān)文章
python通過colorama模塊在控制臺輸出彩色文字的方法
這篇文章主要介紹了python通過colorama模塊在控制臺輸出彩色文字的方法,實例分析了colorama模塊的功能及相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
Python使用Pandas讀取CSV文件數(shù)據(jù)的操作方法
Pandas?是?Python?中一個強(qiáng)大的數(shù)據(jù)分析庫,它提供了大量的工具用于數(shù)據(jù)操作和分析,其中,read_csv?函數(shù)是?Pandas?中最常用的函數(shù)之一,用于從?CSV?文件中讀取數(shù)據(jù),本文將詳細(xì)介紹?read_csv?的基本用法,常見問題及其解決方案,并通過代碼案例進(jìn)行說明2024-12-12
Python使用pickle模塊報錯EOFError Ran out of input的解決方法
這篇文章主要介紹了Python使用pickle模塊報錯EOFError Ran out of input的解決方法,涉及Python異常捕獲操作處理相關(guān)使用技巧,需要的朋友可以參考下2018-08-08
python中實現(xiàn)根據(jù)坐標(biāo)點位置求方位角
這篇文章主要介紹了python中實現(xiàn)根據(jù)坐標(biāo)點位置求方位角方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
django model通過字典更新數(shù)據(jù)實例
這篇文章主要介紹了django model通過字典更新數(shù)據(jù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python調(diào)用shell cmd方法代碼示例解析
這篇文章主要介紹了Python調(diào)用shell cmd方法代碼示例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Python的json.loads() 方法與json.dumps()方法及使用小結(jié)
json.loads() 是一個非常有用的方法,它允許你在處理 JSON 數(shù)據(jù)時,將其轉(zhuǎn)換為 Python 數(shù)據(jù)類型,以便于在代碼中進(jìn)行操作和處理,這篇文章給大家介紹Python的json.loads() 方法與json.dumps()方法及使用小結(jié),感興趣的朋友一起看看吧2024-03-03

