| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import re
- from typing import Dict
- def col_to_num(s):
- n = 0
- for c in s.upper():
- n = n * 26 + ord(c) - 64
- return n
- def num_to_col(n):
- s = ""
- while n:
- n, r = divmod(n - 1, 26)
- s = chr(r + 65) + s
- return s
- def bottom_right(start_cell, data):
- col, row = re.match(r"([A-Za-z]+)(\d+)", start_cell).groups()
- rows = len(data)
- cols = len(data[0]) if rows else 0
- end_col = col_to_num(col) + cols - 1
- end_row = int(row) + rows - 1
- return f"{num_to_col(end_col)}{end_row}"
- def value_covert(value: str, map_dict: Dict[str, str]) -> str:
- if not map_dict:
- return value
- if value not in map_dict:
- return value
- return f"{value}({map_dict[value]})"
- def convert_storage_unit(value, from_unit, to_unit):
- """
- 存储单位换算核心函数
- :param value: 要转换的数值(数字类型)
- :param from_unit: 原始单位(如 B/KB/MB/GB/TB,不区分大小写)
- :param to_unit: 目标单位(如 B/KB/MB/GB/TB,不区分大小写)
- :return: 换算后的结果(保留2位小数)
- """
- # 定义单位与字节(B)的换算比例(1单位 = 多少B)
- unit_map = {
- 'b': 1,
- 'kb': 1024,
- 'mb': 1024 ** 2,
- 'gb': 1024 ** 3,
- 'tb': 1024 ** 4
- }
- # 统一单位为小写,避免大小写问题
- from_unit = from_unit.strip().lower()
- to_unit = to_unit.strip().lower()
- # 校验单位是否合法
- if from_unit not in unit_map or to_unit not in unit_map:
- raise ValueError(f"不支持的单位!仅支持:{list(unit_map.keys())}")
- # 校验数值是否合法
- try:
- value = float(value)
- except ValueError:
- raise ValueError("数值必须是数字(如 1024、2.5 等)")
- # 换算逻辑:先转为字节,再转为目标单位
- bytes_value = value * unit_map[from_unit] # 原始值转字节
- result = bytes_value / unit_map[to_unit] # 字节转目标单位
- return round(result, 2) # 保留2位小数,提升可读性
|