Conversion between PYTHON time and string
This article mainly explains the "PYTHON time and string conversion", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "PYTHON time and string conversion" bar!
1. Convert string to time [in] from datetime import datetime [in] # string [in] time_str = '2019-06-2019: 23 in 57' [in] time_rel = datetime.strptime (time_str,'%Y-%m-%d% Hlav% MHD% S') [in] print (type (time_rel)) [in] print (time_rel) [out] [out] 2019-06-2019: 23:57
Memory method
Strptime: it actually means str parase time. The string is parsed into time.
Note:
The format of the string determines the date format after the strptime.
For example:
[in] time_rel2 = datetime.strptime (time_str,'%Y%m%d%H:%M:%S') [out] ValueError: time data '2019-06-2019: 23 in 57' does not match format'% Y%m%d%H:%M:%S'2, time converted to string # followed by [in] time_str2 = datetime.strftime (time_rel) above '% Y-%m-%d% HGV% MVA% S') [in] print (type (time_str2)) [in] print (time_str2) [out] [out] 2019-06-2019: 23Rd 5 strings but there is no limit to the format of time conversion to strings # obvious Time can be converted into any format of string [in] time_str3 = datetime.strftime (time_rel,'%Y%m%d%H%M%S') [in] print (time_str3) [out] 20190620192357
Memory method
Strftime: it means str from time. The string comes from time. It must be that time is converted into a string.
Thank you for your reading, the above is the content of "PYTHON time and string conversion". After the study of this article, I believe you have a deeper understanding of the problem of PYTHON time and string conversion, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!