python str = ‘2023/11/9 0:00:00’ 转变成‘2023-11-09’

简介: python str = ‘2023/11/9 0:00:00’ 转变成‘2023-11-09’

可以使用datetime模块将字符串转换为指定格式的日期。以下是解析和代码:

解析:

  1. 导入datetime模块
  2. 使用strptime函数将字符串转换为datetime对象
  3. 使用strftime函数将datetime对象转换为指定格式的字符串

代码:

from datetime import datetime

str = '2023/11/9 0:00:00'
date_obj = datetime.strptime(str, '%Y/%m/%d %H:%M:%S')
formatted_str = date_obj.strftime('%Y-%m-%d')
print(formatted_str)
相关文章
|
4月前
|
索引 Python
python字符串(str)
【5月更文挑战第8天】
31 3
|
26天前
|
存储 索引 Python
Python中的str类型
Python中的str类型
|
2月前
|
存储 JSON JavaScript
【Python】已完美解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
【Python】已完美解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
60 1
|
2月前
|
机器学习/深度学习 缓存 安全
Python标准库中的`str`类型有一个`translate()`方法,它用于替换字符串中的字符或字符子集。这通常与`str.maketrans()`方法一起使用,后者创建一个映射表,用于定义哪些字符应该被替换。
Python标准库中的`str`类型有一个`translate()`方法,它用于替换字符串中的字符或字符子集。这通常与`str.maketrans()`方法一起使用,后者创建一个映射表,用于定义哪些字符应该被替换。
|
2月前
|
开发者 Python
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
56 0
|
2月前
|
存储 Python
语音输入,python数据类型,type()用来查看数据类型,数据类型转换,int(x)转整数,float(x)转换为浮点数,str(x),将对象转为字符串,标识符,标识符不允许使用关键字,关键字参考
语音输入,python数据类型,type()用来查看数据类型,数据类型转换,int(x)转整数,float(x)转换为浮点数,str(x),将对象转为字符串,标识符,标识符不允许使用关键字,关键字参考
|
4月前
|
算法 开发者 Python
【Python 基础扫盲 】self参数、__init__方法和.__str__方法的用处和区别?
【Python 基础扫盲 】self参数、__init__方法和.__str__方法的用处和区别?
200 0
|
XML 编解码 数据格式
Python 字符串str详解(超详细)(二)
Python 字符串str详解(超详细)(二)
99 0
|
10月前
|
JSON 数据格式 Python
Python将字符串(str/json)和字典(dict)互转
Python将字符串(str/json)和字典(dict)互转