ValueError:时间数据“11/25/20,5:08:34 PM”与格式 '-%m/-%d/-%y,-%I:%M:%S %p' 不匹配
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的日期格式错误。我可以在您的格式中看到其他
-
分隔符。删除它,一切都会正常工作。您的示例如下:
将生成输出
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:
will generate the output