如何修复列中datetimestamp的解析错误

2024-04-19 03:44:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个.eml文件,我将内容提取到pandas中以进行进一步的数据分析。其中一列是格式为Thu 07 Mar 2019 01:24:41 -0500的日期时间字段

我尝试使用datetime库来剥离日期/时间,并将其存储在单独的列中。代码如下

       df_res.DateTime . // name of the dataframe 

       OUTPUT: 
       0       Thu, 07 Mar 2019 01:24:41 -0500
       1       Wed, 06 Mar 2019 14:40:26 -0800
       2       Tue, 05 Mar 2019 07:00:53 +0000
       3       Wed, 06 Mar 2019 19:40:06 +0000
       4       Mon, 04 Mar 2019 12:57:01 +0000
       5       Tue, 05 Mar 2019 02:40:28 +0000        


      date = datetime.datetime.strptime(df_res['DateTime'], "%a %d %b %Y %H:%M:%S %z"])



      ERROR: KeyError: ('Thu 07 Mar 2019 01:24:41 -0500\t', '%a %d %b %Y %H:%M:%S %z')

我不知道为什么这篇文章没有在新的专栏中告诉我日期和时间。格式似乎正确


Tags: 文件代码内容pandasdfdatetime格式时间