在将日期转换字符串的时候出错了
来源:4-2 时间对象转字符串的用法
3938823180
2021-06-30 13:50:26
# coding:utf-8
from datetime import datetime
from datetime import timedelta
now = datetime.now()
print(now,type(now))
now_str =now.strftime('%y-%m-%d %H:%M:%S')
print(now_str,type(now_str))
print('-------------------------')
now_obj = datetime.strptime(now_str, '%y-%m-%d %H:%M:%S')
print(now_obj)
print('-------------------------------')
three_days = timedelta(days=3)
after3 = now + three_days
print(after3)
stry_after3 = after3.strftime('%y/%m----------------%d %H:%M:%S:')
print(stry_after3)
befor3 = now - three_days
print(befor3)
错误代码:
D:\pythonwork\venv\Scripts\python.exe D:/pythonwork/python_package/package_datetime.py
2021-06-30 13:48:29.874789 <class 'datetime.datetime'>
21-06-30 13:48:29 <class 'str'>
-------------------------
Traceback (most recent call last):
File "D:/pythonwork/python_package/package_datetime.py", line 11, in <module>
now_obj = datetime.strptime(now_str, '%y-%m-%d %H:%M:%S')
File "C:\Users\tianyu\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 14, in <module>
import locale
File "C:\Users\tianyu\AppData\Local\Programs\Python\Python37\lib\locale.py", line 16, in <module>
import re
File "C:\Users\tianyu\AppData\Local\Programs\Python\Python37\lib\re.py", line 125, in <module>
import functools
File "C:\Users\tianyu\AppData\Local\Programs\Python\Python37\lib\functools.py", line 21, in <module>
from collections import namedtuple
File "C:\Users\tianyu\AppData\Local\Programs\Python\Python37\lib\collections\__init__.py", line 22, in <module>
from keyword import iskeyword as _iskeyword
ImportError: cannot import name 'iskeyword' from 'keyword' (D:\pythonwork\keyword.py)
Process finished with exit code 1
1回答
同学,你好!Python3中keyword是python的模块名,在给python文件命名时不能使用python的模块名进行命名。祝学习愉快!
相似问题