ValueError:时间数据“11/25/20,5:08:34 PM”与格式 '-%m/-%d/-%y,-%I:%M:%S %p' 不匹配

发布于 2025-01-13 06:25:48 字数 579 浏览 1 评论 0原文

我正在尝试从 Whatsapp 群组中提取数据进行分析;创建作为工作日(x 轴)和 2 小时时间窗口(y 轴)函数的活动热图,

我认为第一步应该是为每条消息创建一个字典,其中键作为消息的日期,值作为值是消息本身。我尝试使用 strptime 函数从字符串转换日期。我被困了几个小时试图弄清楚我在这里做错了什么:

import datetime

with open('chat.txt','r+', encoding='utf-8') as f:
    content = f.readlines()

dict = {}

for line in content:
    line = line.replace('[','')
    line = line.replace(']',')')
    line = line.replace('\u200e', '')
    line = line.partition(')')
    dict[key] =  datetime.datetime.strptime(line[0], '-%m/-%d/-%y, -%I:%M:%S %p')
    
    






    
       
      


 

im trying to extract data from a whatsapp group for analyzation; creating heatmaps of activity as a function of weekdays (x axis) and 2 hour time windows (y axis),

I figured my first step should be to make a dictionary for each message, having the keys as the date of message, and the value being the message itself. i tried using strptime function to convert the the dates from strings. ive been stuck for hours trying to figure out what im doing wrong here:

import datetime

with open('chat.txt','r+', encoding='utf-8') as f:
    content = f.readlines()

dict = {}

for line in content:
    line = line.replace('[','')
    line = line.replace(']',')')
    line = line.replace('\u200e', '')
    line = line.partition(')')
    dict[key] =  datetime.datetime.strptime(line[0], '-%m/-%d/-%y, -%I:%M:%S %p')
    
    






    
       
      


 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生再见 2025-01-20 06:25:48

您的日期格式错误。我可以在您的格式中看到其他 - 分隔符。删除它,一切都会正常工作。

您的示例如下:

datetime.datetime.strptime('11/25/20, 5:08:34 PM', '%m/%d/%y, %I:%M:%S %p')

将生成输出

datetime.datetime(2020, 11, 25, 17, 8, 34)

Your date format is wrong. I can see additional - separators in your format. Remove that and all will work fine.

Your example in right way below:

datetime.datetime.strptime('11/25/20, 5:08:34 PM', '%m/%d/%y, %I:%M:%S %p')

will generate the output

datetime.datetime(2020, 11, 25, 17, 8, 34)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文