Wie konvertiert Python den Zeitstempel in das Format „12. Juni 2017 18:24“? Danke
import time
timestamp = time.time()
time.strftime('%Y年%m月%d日 %H时%M分', time.localtime(timestamp))
Traceback (most recent call last):
File "<input>", line 1, in <module>
UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal byte sequence
Eine weitere Beschwerde: Beim Öffnen von Segmentfault in Chrome wird der Speicherabsturzfehler angezeigt. Wann wird dieser behoben?
还真被我研究出来种万能方法,据我百度Google貌似这还是世上独一份?
话不多说,下面上解决原理及方案:
官方文档:https://docs.python.org/3/lib...
class
time.
`struct_time`¶The type of the time value sequence returned by
gmtime()
,localtime()
, andstrptime()
. It is an object with a named tuple interface: values can be accessed by index and by attribute name. The following values are present:tm_year
tm_mon
tm_mday
tm_hour
tm_min
tm_sec
strftime()
descriptiontm_wday
tm_yday
tm_isdst
tm_zone
tm_gmtoff
Note that unlike the C structure, the month value is a range of [1, 12], not [0, 11].
In calls to
mktime()
,tm_isdst
may be set to 1 when daylight savings time is in effect, and 0 when it is not. A value of -1 indicates that this is not known, and will usually result in the correct state being filled in.When a tuple with an incorrect length is passed to a function expecting a
struct_time
, or having elements of the wrong type, aTypeError
is raised.看文档可以得知time.localtime()返回的元组结构,我要用到的是年月日时分,前5个,于是乎,代码:
输出:
搞定。
转载请注明出处,谢谢。