简体   繁体   English

从Excel导入日期时如何在Python中保持格式(mm / dd / yyyy)

[英]How to keep format (mm/dd/yyyy) in Python when importing dates from Excel

I have a column in an Excel file that is filled with dates, with the mm/dd/yyyy format. 我在Excel文件中有一列,其中包含日期,格式为mm/dd/yyyy

I import the column into a list in Python using this code: 我使用以下代码将列导入Python中的列表:

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

But I get this: 但是我得到这个:

[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]

How can I import these dates and keep their original formatting instead of getting these numeric values? 如何导入这些日期并保持其原始格式,而不是获取这些数值?

Are these timestamps? 这些是时间戳吗? If so, you can convert them into dates. 如果是这样,您可以将它们转换为日期。 This may help: 这可能会有所帮助:

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

You will get: 你会得到:

['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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在2014年1月4日给出时如何将mm / dd / yyyy格式转换为python 3.0中的yyyy-mm-dd - How to convert from mm/dd/yyyy format to yyyy-mm-dd in python 3.0 when given 1/4/2014 对于1970年之前的日期,Python将日期格式从dd / mm / yy更改为dd / mm / yyyy - Python change the date format from dd/mm/yy to dd/mm/yyyy for the dates before 1970 如何将python熊猫中的日期格式从mm / dd / yyyy更改为dd-mmm-yyyy - How to change date format in python pandas from mm/dd/yyyy to dd-mmm-yyyy Python Dataframe 日期列 - 将格式更改为 yyyy-mm-dd - Python Dataframe column of dates - change format to yyyy-mm-dd 如何在 Python 数据帧中将时间戳列格式从 [YYYY/MM/DD] 转换为 [DD/MM/YYYY]? - How can I can convert a timestamp column format from [YYYY/MM/DD] to from [DD/MM/YYYY] in Python data frame? 如何从 09-Jan-2019 格式的 excel 中解析日期并使用 python 将其转换为 yyyy-mm-dd 格式 - how can I parse a date from excel in 09-Jan-2019 format and convert it into yyyy-mm-dd format using python 如何在 pyspark 数据框中以“DD/MM/YYYY”格式转换日期? - How to convert dates in "DD/MM/YYYY" format in a pyspark dataframe? 如何基于 RIDE python 机器人框架将日期格式从 dd/mm/yyyy 转换为 yyyy-mm-dd - How to convert date format from dd/mm/yyyy to yyyy-mm-dd base on RIDE python robot framework 将日期列表从 YYYY/MM/DD 更改为 DD/MM/YYYY - Change list of dates from YYYY/MM/DD to DD/MM/YYYY 如何在 python 中使用正则表达式来验证 MM/DD/YYYY 格式的日期? - How can I use regex in python to validate dates in the MM/DD/YYYY format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM