我尝试从日期中删除秒数:
>>> import datetime
>>> test1 = datetime.datetime(2011, 6, 10, 0, 0)
>>> test1
datetime.datetime(2011, 6, 10, 0, 0)
>>> str(test1)
'2011-06-10 00:00:00'
>>> str(test1).rstrip('00:00:00')
'2011-06-10 '
>>> str(test1).rstrip(' 00:00:00')
'2011-06-1'
为什么删除“10”末尾的 0?
最佳答案
str.rstrip()
不会删除确切的字符串——它会删除字符串中出现的所有字符。由于您知道要删除的字符串的长度,因此您可以简单地使用
str(test1)[:-9]
甚至更好
test1.date().isoformat()
关于python .rstrip 删除一个额外的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6311968/