pandas 基础
Pandas 第1行为title,所以数据时从第二行开始读,第二行为第0行,第一列是第0列,要和openpyxl区分
df = pd.read_excel("test_data.xlsx") #读取EXCEL,默认读第0列 df = pd.read_excel("test_data.xlsx",sheet_name="login") print(df.values) #读取所有行所有列 print(df.iloc[1].values) #读取指定行 并以列表输出 print(df.iloc[1,1]) #读取指定行指定列 读取的是第二行第二列 print(df.loc[1,["url","data"]].to_dict()) #以列名读取指定行列数据,以字典输出 print(df["url"].values) #读取指定列 print(df.iloc[1].values) #读取指定行 print(df.index.values) #获取索引值
import pandas as pd df = pd.read_excel("case.xlsx",sheet_name="login") print("======================>>>>>>>>>>>>>>1") print(df) #读取整个excel以矩阵形式打印 print("======================>>>>>>>>>>>>>>2") print(df.loc[:,"url":"method"]) #loc[,] 列表中左边是行,右边是列, :号左右空代表所有,写列名指定左右边界 print("======================>>>>>>>>>>>>>>3") print(df.loc[:,"id":"expect"].values) # .values 结果输出为矩阵形式的列表 print("======================>>>>>>>>>>>>>>4") print(df.loc[:,"id":"expect"].to_dict()) #to_dict 结果输出为字典,列名为key,value为列名对应所有的值的字典 print("======================>>>>>>>>>>>>>>5") print(df.loc[1].to_dict()) #指定行后,结果为列表嵌套字典,key为列名,value为指定行的值,符合我们要的形式 print("======================>>>>>>>>>>>>>>6") print(df.index.values) #打印行数 print("======================>>>>>>>>>>>>>>7") #循环获取每一行的数据到字典,添加到列表中 test_data = [] for i in df.index.values: row_data = df.loc[i,"id":"expect"].to_dict() test_data.append(row_data) print(test_data) print("======================>>>>>>>>>>>>>>8") xl = pd.ExcelFile('case.xlsx') print(xl.sheet_names) # 获取表格里所有的sheet名,存放到一个列表显示
输出:
======================>>>>>>>>>>>>>>1 id url method data expect 0 1 http://172.16.20.25:82/center/login Post text1 NaN 1 2 http://172.16.20.25:82/center/login Post text2 "错误" 2 3 http://172.16.20.25:82/center/login Post text3 "错误" ======================>>>>>>>>>>>>>>2 url method 0 http://172.16.20.25:82/center/login Post 1 http://172.16.20.25:82/center/login Post 2 http://172.16.20.25:82/center/login Post ======================>>>>>>>>>>>>>>3 [[1 'http://172.16.20.25:82/center/login' 'Post' 'text1' nan] [2 'http://172.16.20.25:82/center/login' 'Post' 'text2' '"错误"'] [3 'http://172.16.20.25:82/center/login' 'Post' 'text3' '"错误"']] ======================>>>>>>>>>>>>>>4 {'id': {0: 1, 1: 2, 2: 3}, 'url': {0: 'http://172.16.20.25:82/center/login', 1: 'http://172.16.20.25:82/center/login', 2: 'http://172.16.20.25:82/center/login'}, 'method': {0: 'Post', 1: 'Post', 2: 'Post'}, 'data': {0: 'text1', 1: 'text2', 2: 'text3'}, 'expect': {0: nan, 1: '"错误"', 2: '"错误"'}} ======================>>>>>>>>>>>>>>5 {'id': 2, 'url': 'http://172.16.20.25:82/center/login', 'method': 'Post', 'data': 'text2', 'expect': '"错误"'} ======================>>>>>>>>>>>>>>6 [0 1 2] ======================>>>>>>>>>>>>>>7 [{'id': 1, 'url': 'http://172.16.20.25:82/center/login', 'method': 'Post', 'data': 'text1', 'expect': nan}, {'id': 2, 'url': 'http://172.16.20.25:82/center/login', 'method': 'Post', 'data': 'text2', 'expect': '"错误"'}, {'id': 3, 'url': 'http://172.16.20.25:82/center/login', 'method': 'Post', 'data': 'text3', 'expect': '"错误"'}] ======================>>>>>>>>>>>>>>8 ['login', 'Sheet1', 'Sheet2', 'Sheet3']
do_excel.py
from tools.read_config import ReadConfig from tools import project_path from tools.get_data import GetData import pandas as pd
class DoExcel:
def get_data(self,file_name):
mode = eval(ReadConfig().get_config("case.config", "MODE", "mode"))
print("mode是{}".format(mode))
test_data = []
for key in mode:
print("当前的key: = {0}".format(key))
df = pd.read_excel(file_name, sheet_name=key)
if mode[key] =='all':
print("进入:{0}".format(mode[key]))
for i in df.index.values: #df.index.values为sheet的行号列表
row_data = df.loc[i].to_dict() #循环读取每行的数据并转化为字典
test_data.append(row_data) #将字典放入列表
else:
for case_id in mode[key]:
print("进入的key: = {0}".format(key))
print("没进入all的列表是{0}".format(mode[key]))
row_data=df.loc[case_id].to_dict()
test_data.append(row_data)
return test_data
if __name__ == '__main__': do_excel = DoExcel() test_data = do_excel.get_data(r"D:BaiduNetdiskDownloadNMB_API_61 est_data est_data.xlsx") print(test_data)
参考 :https://www.jb51.net/article/166820.htm