# !/usr/bin/env python3
# encoding: utf-8
"""
@desc:file相关操作
"""
import os
import chardet
import datetime
from typing import List, Optional
def get_huge_file_line_nums(file:str)->int:
"""
Get file all lines nums
:param file: file path
:return: file all lines nums
"""
with open(file, 'rb') as f:
for count, _ in enumerate(f, 1):
pass
return count
def get_head_tail(file:str,layer:int)->tuple:
"""
:param file: file path
:param layer: Level series
return (,,)
获取文件路径指定层级的父路径,层级名,问今名
"""
filename = ""
len_file = len(file.split(os.path.sep))
if layer <= len_file:
for i in range(0,layer):
file, tail = os.path.split(file)
if i == 0:
filename = tail
return file,tail,filename
else:
return ("要获取的层级超过路径层级长度",'',)
def get_small_file_line_nums(file:str):
"""
Get file all lines nums
:param file: file path
:return: file all lines nums
"""
return sum(1 for _ in open(file, 'rb'))
def rename(old_file:str, new_file:str):
"""
Rename in place file
:param file:old file ,new file
:return:
"""
try:
if os.path.isfile(old_file) and not os.path.exists(new_file):
os.renames(old_file, new_file)
except Exception as e:
print(e, e.__traceback__.tb_lineno)
def make_map(file, header: Optional[List[str]] = None, spliter: str = "\t"):
"""
Make json ,k:Column Header,v:A List Of All Elements Except The Column Header
:param file:File wait to Process
:param header: File Header
:param spliter: Line Separator
:return: Result json
"""
map = {}
encod = get_encoding(file)
with open(file, "r", encoding=encod)as fp:
if header is None:
header = fp.readline().split(spliter)
for line in fp:
lis = line.strip("\n").split(spliter)
for i in range(len(header)):
if map.get(header[i]) is not None:
map[header[i]].append(lis[i])
else:
map[header[i]] = [lis[i]]
return map
def make_map_two_clo(file, spliter: str = "\t"):
"""
build map with two columns
:param file: File wait to Process
:param spliter: Line Separator
:return: Result json
"""
map = {}
encod = get_encoding(file)
with open(file, "r", encoding=encod)as fp:
for line in fp:
lis = line.strip("\n").split(spliter)
if map.get(lis[0]) is not None:
print("{} 重复".format(lis[0]))
else:
map[lis[0]] = lis[1]
return map
def get_encoding(file: str) -> str:
"""
Get text file encoding format
:param file: file path
:return: Coding
"""
with open(file, "rb")as fp:
fp_bit = fp.read(1024 * 1024)
encoding = chardet.detect(fp_bit)
return encoding["encoding"]
def get_file_detail(file: str, unit: str = "MB") -> str:
"""
get file size
windows the default displayed by the system is 1000
:param file: file path
:return: file size
"""
file_size = os.path.getsize(file)
if unit == "KB":
return str(round(file_size / float(1024), 2)) + " " + unit
elif unit == "MB":
return str(round(file_size / float(1024 * 1024), 2)) + " " + unit
def get_hierarchy_info(file:str, parent_path:str, hierarchy:int):
"""
获取指定层级的文件 parent_path,name
:param file:
:param parent_path:
:param hierarchy:
:return:
"""
flag = False
_file = file.replace(parent_path + "\\", "")
for i in range(0, hierarchy):
_file, tail = os.path.split(_file)
while not flag:
flag = True
name = tail
return _file, tail, name
def add_key(map:dict, key:str, value)->dict:
"""
向map中添加键值对
:param map:
:param key:
:param value:
:return:
"""
if map.get(key) is not None:
map[key] = map[key] + value
else:
map[key] = value
if isinstance(value,int):
if map.get("total_num") is None:
map["total_num"] = value
else:
map["total_num"] = map["total_num"] + value
return map
# !/usr/bin/env python3
# encoding: utf-8
"""
@desc:path相关操作
"""
import os
def create_path(path):
"""
Check path exists ,if not exists make it
:param path: path
:return:
"""
os.makedirs(path,exist_ok=True)
def get_files_under_path(path):
"""
获取路径下所有文件的绝对路径
:param path: 根路径
:return: 路径下文件绝对路径列表
"""
try:
if os.path.exists(path) and os.path.isabs(path):
files_path_lis = []
for path, dir_lis, file_lis in os.walk(path):
if len(file_lis) > 0:
for file in file_lis:
files_path_lis.append(os.path.join(path, file))
return files_path_lis
except Exception as e:
print(e, e.__traceback__.tb_lineno)
# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@des : excel相关处理 .xls后缀
"""
import xlsxwriter
import xlrd
import re
from typing import Dict, Optional
def write_excel(data: Dict, file, sheet='Sheet1', rule: Optional = None,
style_header: Optional = None, style: Optional = None):
"""
Write Excel Operation
:param dic: k:header,value:a List Of Column Texts Corresponding To The Header
:param out_file:output_file_location
:param style_header:Header Style
:param style:Content Style
:return:
"""
workbook = xlsxwriter.Workbook(file)
worksheet = workbook.add_worksheet(sheet)
headers = list(data.keys())
for i in range(len(headers)):
if style_header is not None:
_style_header = workbook.add_format(dict(style_header))
worksheet.write_string(
row=0,
col=i,
string=headers[i],
cell_format=_style_header
)
else:
worksheet.write_string(
row=0,
col=i,
string=headers[i]
)
for k, v in data.items():
col = headers.index(k)
for j in range(len(v)):
if rule is None:
string = v[j]
else:
string = re.sub(rule, "", v[j])
if style is not None:
_style = workbook.add_format(dict(style))
worksheet.write_string(
row=j + 1,
col=col,
string=string,
cell_format=_style
)
else:
worksheet.write_string(
row=j + 1,
col=col,
string=string
)
workbook.close()
def read_excel(file: str, sheet="Sheet1") -> dict:
"""
Read Excel
:param file:pending_file_path
:param sheet: sheet_name
:return: dict number_of_rows_and_columns_in_excel, columns dict,row dict
"""
data = xlrd.open_workbook(file)
table = data.sheet_by_name(sheet) # 通过名称来获取指定页
nrows = table.nrows # 为行数,整形
ncolumns = table.ncols # 为列数,整形
# print(nrows,ncolumns)
# 输出每列为一个列表
def get_col_lis(nrows, table):
clo_dic = {}
for row in range(nrows):
row_info = table.row_values(row)
clo_dic[str(row)] = row_info
return clo_dic
# 输出每行为一个列表
def get_row_lis(ncolumns, table):
row_dic = {}
for row in range(ncolumns):
row_info = table.col_values(row)
row_dic[str(row)] = row_info
return row_dic
res = {
"shape": (nrows, ncolumns),
"col": get_col_lis(nrows, table),
"row": get_row_lis(ncolumns, table)
}
return res
# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@des : 时间转换相关处理
"""
import time
def timestamp_to_time(timestamp: float) -> str:
"""
Convert timestamp to standard time format
:param timestamp:timestamp
:return: standard time:%Y-%m-%d %H:%M:%S
"""
time_struct = time.localtime(timestamp)
return time.strftime("%Y-%m-%d %H:%M:%S", time_struct)
def time_to_timestamp(time_str: str) -> float:
"""
Convert string format time to timestamp
:param time: String format time:"2022-09-15 00:00:00",
:return: Timestamp
"""
struct_time = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
return time.mktime(struct_time)
# !/usr/bin/env python3
# encoding: utf-8
"""
@desc:md5相关操作
"""
import hashlib
import requests
def make_md5(item: str, salt: bytes = b""):
"""
计算输入文本的 md5
:param item:输入的文本
:return: 输入的文本对应的md5
"""
item = item.rstrip("\n")
md5_machine = hashlib.md5(salt)
md5_machine.update(item.encode('utf-8'))
return md5_machine.hexdigest()
def md5_decode(secret):
"""
md5 解密接口 抓取自 http://www.ttmd5.com/
只能解析不带盐值的
:param secret:待解密字符串
:return:
True:
{
"cipher": "49ba59abbe56e057",
"flag": 1,
"plain": "123456",
"type": "",
"time": 0.0042200088500977,
"msg": ""
}
False:
flag != 1 都是破解失败
"""
host = "http://www.ttmd5.com"
url = "{}/do.php?c=Decode&m=getMD5&md5={}".format(host, secret)
res = requests.get(url)
return res.json()
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/156882.html