代码拉取完成,页面将自动刷新
# -*- 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'))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。