2017-07-14 241 views
0

在Excel文件中填充日期的格式爲mm/dd/yyyy格式。從Excel導入日期時如何在Python中保留格式(mm/dd/yyyy)

我輸入列到名單在Python使用此代碼:

first_excel_file = pd.read_excel('test.xlsx') 
item_end_date = first_excel_file['Item End Date'].values.tolist() 

但我得到這個:

[1478476800000000000, 1476921600000000000, 1488240000000000000, 1488240000000000000, 1488240000000000000, 1488326400000000000, 1489622400000000000, 1489622400000000000, 1489968000000000000, 1494288000000000000, 1454198400000000000, 1454198400000000000, 1490918400000000000, 1490918400000000000, 1490918400000000000, 1491955200000000000, 1491955200000000000, 1446249600000000000, 1509408000000000000, 1509408000000000000, 1509408000000000000, 1364688000000000000, 1391126400000000000, 1398816000000000000, 1422662400000000000, 1418428800000000000, 1419292800000000000, 1422662400000000000, 1422662400000000000, 1422662400000000000, 1423612800000000000, 1426291200000000000, 1438300800000000000] 

如何導入這些日期並保持其原始格式,而不是領這些數值?

+0

這將幫助,如果你表現出你目前正在使用加載該文件中的代碼,並指定文件格式是什麼。 (Excel接受很多不同的格式。) –

+0

請注意,excel中顯示的日期格式與csv一樣很少與文件格式相匹配。它是一個實際的Excel文件格式還是在Excel中打開的不同格式? – roganjosh

+0

@roganjosh它是一個實際的excel文件,有人創建 –

回答

1

這些時間戳嗎? 如果是這樣,你可以將它們轉換成日期。 這可以幫助:

from datetime import datetime 
item_end_date = [datetime.fromtimestamp(adt//1000000000).strftime("%m/%d/%Y") 
       for adt in item_end_date] 

您將獲得:

['11/06/2016', '10/19/2016', '02/27/2017', '02/27/2017', '02/27/2017', 
'02/28/2017', '03/15/2017', '03/15/2017', '03/19/2017', '05/08/2017', 
'01/30/2016', '01/30/2016', '03/30/2017', '03/30/2017', '03/30/2017', 
'04/11/2017', '04/11/2017', '10/30/2015', '10/30/2017', '10/30/2017', 
'10/30/2017', '03/30/2013', '01/30/2014', '04/29/2014', '01/30/2015', 
'12/12/2014', '12/22/2014', '01/30/2015', '01/30/2015', '01/30/2015', 
'02/10/2015', '03/13/2015', '07/30/2015'] 
+0

item_end_date = [datetime.fromtimestamp(adt // 1000000000).strftime(「%m /%d /%Y」)for item_end_date] –

+0

只是爲了降低O ()要求,一次完成! –

+0

好的,我會編輯它。謝謝@AlanLeuthard – Huang

相關問題