2024-11-19
====================================
数字转换为字符
num = 123
char = str(num)
print(char) # 输出: '123'
====================================
字符串转换为float
s = "123.456"
f = float(s)
print(f)
====================================
字符转数字
方法一:使用内置的int()函数
number_str = "123"
number = int(number_str)
print(number)
方法二:使用eval()函数
number_str = "123"
number = eval(number_str)
print(number)
方法三:使用math库中的math.floor()函数
import math
number_str = "123"
number = math.floor(float(number_str))
print(number)
======================================
字符转日期
from datetime import datetime
date_str = '2023-04-01'
date_format = '%Y-%m-%d'
# 使用strptime转换
date_obj = datetime.strptime(date_str, date_format)
print(date_obj) # 输出: 2023-04-01 00:00:00
======================================
# ISO字符转日期
date_object = isoparse("2024-11-19T23:24:15.6131472+08:00")
formatted_date=date_object.strftime("%Y-%m-%d %H:%M:%S")
转换后结果:2024-11-19 23:24:15