英文:
Python TypeError: 'datetime.datetime' object is not subscriptable. Unable to print desired number
问题
使用这段代码,我试图从这个日期中提取只有日期号码。在这个示例中,它应该是30。当我尝试使用range[]函数打印时,我遇到了错误。如何只打印这个期望的数字?
我无法将其转换为str函数或使用range函数来选择我想要的数字。
英文:
Using this code I am trying to extract just the day number from this date. In this example it would be 30. When I try to print with the range[] function I get the error. How can I just print this desired number?
I am unable to turn into a str function or use the range function to select my desired number.
答案1
得分: 1
datetime对象具有用于其组件的命名属性,因此您只需引用.day
!
>>> datetime.datetime.now().day
10
您可以使用dir()
查看对象的所有方法和属性。
>>> import datetime
>>> from datetime import timezone
>>> d = datetime.datetime.now(tz=timezone.utc)
>>> dir(d)
['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold', 'fromisocalendar', 'fromisoformat', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']
英文:
datetime objects have named properties for their components, so you should be able to just reference .day
!
>>> datetime.datetime.now().day
10
You can see all the methods and properties of an object with dir()
>>> import datetime
>>> from datetime import timezone
>>> d = datetime.datetime.now(tz=timezone.utc)
>>> dir(d)
['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold', 'fromisocalendar', 'fromisoformat', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']
答案2
得分: 1
如果您想提取日期,请使用datetime模块中的.day。
dt_m = datetime.datetime.fromtimestamp(m_time)
print(dt_m.day)
输出将是日期,或在您的情况下是30。
英文:
If you are trying to extract the day, you should use .day from the datetime module.
dt_m = datetime.datetime.fromtimestamp(m_time)
print(dt_m.day)
The output will be the day, or in your case 30.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论