2016-07-01 19 views
-3

我的字符串是:Nov 05 2016 01:30:00PM 我需要刪除前導月和小時 0輸出應該像Nov 5 2016 1:30:00PM在Windows中刪除在一個月前導零的日期蟒蛇

添加%和字母之間的連字符,你不能刪除前導零窗口..所以我不能使用該選項。

import time 
from_date="2016-10-02T01:45:00" 
conv=time.strptime(from_date,"%Y-%m-%dT%H:%M:%S") 
conDateTime = time.strftime("%b %d %Y %I:%M:%S%p",conv) 
print conDateTime 
day = time.strftime("%d",conv).lstrip('0') 
hour = time.strftime("%H",conv).lstrip('0') 
conDateTime = conDateTime.replace(time.strftime("%d",conv), day) 
conDateTime = conDateTime.replace(time.strftime("%I",conv), hour) 
print conDateTime 

出放:10月2日216上午01點45分00秒 它消除年份和月份的0

conDateTime = conDateTime.replace(time.strftime("%d",conDateTime), day) 

給出以下錯誤類型錯誤:參數必須是9項順序,不str的

+0

加入你試過到目前爲止 –

+1

試試這個代碼是什麼,請給予更詳細的'打印工作的一種方式格式(conv.tm_hour)+ time.strftime(「:%M:%S%p」,轉換) 「time.strftime(」%b%e%Y「,conv)+」{0:d} – Serenity

回答

0

我不認爲有一個簡單的方法來做到這一點。您需要分別轉換零件,然後將它們連接在一起。這裏是做哪些關於Python 2和Python 3

import time 

time_formats = ('%b', '%d %Y', '%I:%M:%S%p') 

def strip_time(fmt, t): 
    return time.strftime(fmt, t).lstrip('0') 

from_date = "2016-10-02T01:45:00" 
conv = time.strptime(from_date, "%Y-%m-%dT%H:%M:%S") 
conDateTime = [strip_time(fmt, conv) for fmt in time_formats] 
print(' '.join(conDateTime)) 

輸出

Oct 2 2016 1:45:00AM