1 Star 0 Fork 0

binyu/locustProject

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
util.py 3.73 KB
一键复制 编辑 原始数据 按行查看 历史
liubinyu1 提交于 2024-03-09 18:36 . 完成
# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2023-06-14
desc:
sample:
"""
import csv
import hashlib
import os
import openpyxl
import pandas as pd
def get_file_path(filename):
return os.path.join(os.path.dirname(__file__), "files", filename)
def get_md5(data):
md5 = hashlib.md5()
md5.update(data.encode('utf-8'))
return md5.hexdigest()
# [[],[]]
# [1,2,3]
def getcsv_list(path, header=True):
list = []
with open(get_file_path(path), 'r', encoding='utf-8') as f:
reader = csv.reader(f)
if header:
next(reader) # 跳过头部
for line in reader: # line-> list
if len(line) > 1:
list.append(line)
else:
list.append("".join(line))
return list
def getcsv_list_pd(path, header=True):
if header:
read_csv= pd.read_csv(get_file_path(path), encoding='utf-8')
else:
read_csv = pd.read_csv(get_file_path(path), encoding='utf-8',header=None)
return read_csv.values.tolist()
# [{},{}]
def getcsv_dic(path):
list = []
with open(get_file_path(path), 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for dic in reader: # line-> dic
list.append(dic)
return list
def getcsv_dic_pd(path):
read_csv= pd.read_csv(get_file_path(path), encoding='utf-8')
list_dic = []
header = list(read_csv.columns)
data_list= read_csv.values.tolist()
for data in data_list:
list_dic.append(dict(zip(header,data)))
return list_dic
# [[],[]]
def getexcel_list(path, sheet_index=0, header=True):
workbook = openpyxl.load_workbook(get_file_path(path))
worksheet = workbook.worksheets[sheet_index] # 第一个
list = []
if header:
rows = worksheet.iter_rows(min_row=2)
else:
rows = worksheet.iter_rows(min_row=1)
for row in rows:
list_row = []
for cell in row:
list_row.append(cell.value)
list.append(list_row)
return list
def getexcel_list_pd(path, sheet=0,header=True):
if header:
read_excel= pd.read_excel(get_file_path(path),sheet)
else:
read_excel = pd.read_excel(get_file_path(path),sheet,header=None)
return read_excel.values.tolist()
# [{},{}]
def getexcel_dic(path, sheet_index=0):
workbook = openpyxl.load_workbook(get_file_path(path))
worksheet = workbook.worksheets[sheet_index] # 第一个
frist_row = worksheet[1]
frist_header = []
for cell in frist_row:
frist_header.append(cell.value)
list_data = []
for row in worksheet.iter_rows(min_row=2):
list_row = []
for cell in row:
list_row.append(cell.value)
dic_data = dict(zip(frist_header, list_row))
list_data.append(dic_data)
return list_data
def getexcel_dic_pd(path, sheet=0):
read_excel= pd.read_excel(get_file_path(path),sheet)
list_dic = []
header = list(read_excel.columns)
data_list = read_excel.values.tolist();
for data in data_list:
list_dic.append(dict(zip(header, data)))
return list_dic
if __name__ == '__main__':
# print(get_md5("test"))
# print(getcsv_list("case.csv"))
# print(getcsv_list("id.txt",header=False))
# print(getcsv_list("case2.csv",header=False))
# print(getcsv_dic('case.csv'))
# print(getexcel_list('case.xlsx'))
# print(getexcel_list('case2.xlsx',sheet_index=1,header=False))
#print(getexcel_dic('case.xlsx'))
# print(getexcel_list_pd('case.xlsx'))
# print(getexcel_list_pd('case2.xlsx',sheet=1,header=False))
print(getexcel_dic_pd('case.xlsx'))
# a = ['a', 'b', 'c']
# b = ['1', 2, '3']
# print(list(zip(a, b)))
# print(dict(zip(a, b)))
# httprunner yaml/json api
print(getcsv_dic_pd('case.csv'))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liubinyu19930105/locust-project.git
git@gitee.com:liubinyu19930105/locust-project.git
liubinyu19930105
locust-project
locustProject
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385