本博文将使用python对长度单位、时间单位、重量单位、字节单位等实现单位换算。
1、长度单位
示例代码:
import re
def length_conversion(num: str, unit: str = 'm'):
"""长度单位由大到小包括:千米(km)、米(m)、分米(dm)、厘米(cm)、毫米(mm)"""
dic_conversion = {'km': 1000 * 10 * 10 * 10, 'm': 10 * 10 * 10, 'dm': 10 * 10, 'cm': 10, 'mm': 1}
try:
unit_name = re.findall(r"[^\d].*", num)[0].strip().lower() # 匹配不以数字开头的字符串
num_value = int(re.findall(f"\d*", num)[0])
except Exception as e:
print(e)
return f"您输入的数值:{num}格式错误!"
if unit_name.lower() not in ['km', 'm', 'dm', 'cm', 'mm']:
return f"您输入的数值:{num}格式错误!"
if unit_name == 'km':
mm_value = num_value * dic_conversion['km']
elif unit_name == 'm':
mm_value = num_value * dic_conversion['m']
elif unit_name == 'dm':
mm_value = num_value * dic_conversion['dm']
elif unit_name == 'cm':
mm_value = num_value * dic_conversion['cm']
else:
mm_value = num_value
return mm_value / dic_conversion[unit]
if __name__ == '__main__':
num = '65Km'
res = length_conversion(num, 'm')
print(res, type(res))
print(length_conversion('65m'))
print(length_conversion('65dm'))
print(length_conversion('65cm'))
print(length_conversion('65mm'))
运行结果:
2、时间单位
示例代码: 【年月转换目前存在问题】
import re
def time_conversion(num: str, unit: str = 's'):
"""时间单位由大到小包括:年(year)、月(month)、日(day)、时(hour)、分(minute)、秒(second)"""
"""一月默认30天计算,一年365年"""
tips = '年(year)、月(month)、日(day)、时(hour)、分(minute)、秒(second)'
dic_conversion = {'year': 365 * 24 * 60 * 60, 'month': 30 * 24 * 60 * 60, 'day': 24 * 60 * 60, 'hour': 60 * 60, 'minute': 60, 'second': 1}
try:
unit_name = re.findall(r"[^\d].*", num)[0].strip().lower() # 匹配不以数字开头的字符串
num_value = int(re.findall(f"\d*", num)[0])
except Exception as e:
print(e)
return f"您输入的数值:{num}格式错误!请参考:{tips}"
if unit_name.lower() not in ['year', 'month', 'day', 'hour', 'minute', 'second']:
return f"您输入的数值:{num}格式错误!请参考:{tips}"
if unit_name == 'year':
mm_value = num_value * dic_conversion['year']
elif unit_name == 'month':
mm_value = num_value * dic_conversion['month']
elif unit_name == 'day':
mm_value = num_value * dic_conversion['day']
elif unit_name == 'hour':
mm_value = num_value * dic_conversion['hour']
elif unit_name == 'minute':
mm_value = num_value * dic_conversion['minute']
else:
mm_value = num_value
return mm_value / dic_conversion[unit]
if __name__ == '__main__':
num = '2year'
res = time_conversion(num, 'day')
print(res, type(res))
print(time_conversion('2day', 'second'))
print(time_conversion('2hour', 'second'))
print(time_conversion('2minute', 'second'))
print(time_conversion('2second', 'second'))
运行结果;
3、字节单位
示例代码:
import re
def bytes_conversion(num: str, unit: str = 'M'):
"""字节单位由大到小包括:B、K、M、G、T、P"""
tips = 'K、M、G、T、P'
dic_conversion = {'K': 1, 'M': 1024 ** 1, 'G': 1024 ** 2, 'T': 1024 ** 3, 'P': 1024 ** 4}
try:
unit_name = re.findall(r"[^\d].*", num)[0].strip().upper()[0] # 匹配不以数字开头的字符串
num_value = int(re.findall(f"\d*", num)[0])
except Exception as e:
print(e)
return f"您输入的数值:{num}格式错误!请参考:{tips}"
if unit_name not in ['B', 'K', 'M', 'G', 'T', 'P']:
return f"您输入的数值:{num}格式错误!请参考:{tips}"
if unit_name == 'P':
mm_value = num_value * dic_conversion['P']
elif unit_name == 'T':
mm_value = num_value * dic_conversion['T']
elif unit_name == 'G':
mm_value = num_value * dic_conversion['G']
elif unit_name == 'M':
mm_value = num_value * dic_conversion['M']
else:
mm_value = num_value
return mm_value / dic_conversion[unit[0].upper()]
if __name__ == '__main__':
num = '2GB'
res = bytes_conversion(num, 'm')
print(res, type(res))
print(bytes_conversion('1024m', 'GB'))
print(bytes_conversion('1m', 'K'))
运行结果;