我从JIRA API动态创建并更新了一些日期时间,用于自动化过程,该过程每月使用python脚本加载到excel工作表中,以每月获取JIRA票证。
一些采样日期如下:
我要呈现的格式为%d-%b-%Y(例如:2017年2月9日)
我尝试使用python datetime.strptime
和strftime
。
示例代码如下:
from datetime import datetime
datetimeObj = datetime.strptime('2017-02-09T09:43:04.216+1000', '%Y-%m-%dT%H:%M:%S.%f+1000')
print(datetimeObj.strftime('%d-%b-%Y'))
这给了我预期的结果,但是现在我已经硬编码了我想要的日期之类的值,并以datetime格式添加了+1000
,但是由于它的动态变化的datetime,当datetime以这样的值结尾时,我会卡住为+1100
。
我相信最后一部分与夏令时有关,但是在这种情况下,我找不到适合的格式。
总有没有为最后+1000
和+1100
部分转换日期时间格式,而不是像'%Y-%m-%dT%H:%M:%S.%f+1000'
这样进行硬编码?
答案 0 :(得分:2)
似乎您已经找到了解决方案,但是如果这始终是格式。
您可以这样做:
date = '2017-02-09T09:33:33.508+1100'
d = re.search('[0-9]+-[0-9]+-[0-9]+',date).group()
d = re.search('([0-9]+-)+[0-9]+',date).group()
d = date.split('T')[0]
d = date[0:10]
或此的某种变体...然后:
print(datetime.strptime(d,'%Y-%m-%d').strftime('%d-%b-%Y'))
或
print(datetime.strptime(''.join(re.findall('\d+',date)[0:3]),'%Y%m%d').strftime('%d-%b-%Y'))
:)
答案 1 :(得分:1)
刚刚找到了答案。希望如果有人像我一样被困住,这也会对其他人有所帮助。
import pandas as pd
import matplotlib.pyplot as plt
#only if use jupyter notebook
%matplotlib inline
a = pd.DataFrame({"label":["a","b","c","d"],
"value1":[2,4,6,8],
"value2":[11,12,13,14],
"value3":[5,6,7,8]})
fig = plt.figure(figsize=(20,20))
ax=plt.subplot(2, 2, 1)
plt.plot(a.label, a.value1, "r--", linewidth=5, label="a")
plt.plot(a.label, a.value2, "b-", linewidth=5, label="b")
plt.plot(a.label, a.value3, "g-", linewidth=5, label="c")
#grid on
plt.grid()
#change size of legend
ax.legend(fontsize=20)
#hiding upper and right axis layout
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
#changing the thickness
ax.spines['bottom'].set_linewidth(3)
ax.spines['left'].set_linewidth(3)
#size of axes
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
#setlabels
plt.show()
使用from datetime import datetime
datetimeObj = datetime.strptime('2017-02-09T09:43:04.216+1000', '%Y-%m-%dT%H:%M:%S.%f%z')
print(datetimeObj.strftime('%d-%b-%Y'))
将解决此问题。
%z
是python %z
和strftime()
中的UTC偏移量,格式为+ HHMM或-HHMM(如果对象是天真对象,则为空字符串)。
当时间格式末尾有strptime()
之类的值时,这很有帮助。