我试图从聊天日志文件中删除我的对话,只分析其他人的数据。当我像这样将文件加载到Python中时:
with open(chatFile) as f:
chatLog = f.read().splitlines()
数据是这样加载的(比示例长得多):
^{pr2}$我希望它看起来像这样:
'Other person's name',
'08:39 Chat Data....',
'08:40 Chat data...,
'08:40 Chat data...?',
我在考虑对正则表达式使用if语句:
name = 'My Name'
for x in chatLog:
if x == name:
"delete all data below until you get to reach the other
person's name"
我不能让代码正常工作,有什么想法吗?在
我想你误解了“正则表达式”的意思。。。这并不意味着您只需编写英语指令,python解释器就能理解它们。或者您使用的是伪代码,这使得调试变得不可能。在
如果你没有对方的名字,我们可以假设它不是以数字开头的。假设所有非名称行都以数字开头,如您的示例所示:
您可以使用带有空字符串的^{} 作为第二个参数(即替换字符串)删除所有邮件。在
假设每条聊天消息都以时间戳开头的新行开始,并且没有人的名字可以以数字开头,那么正则表达式模式
re.escape(yourname) + r',\n(?:\d.*?\n)*'
应该与所有消息匹配,然后这些匹配项可以替换为空字符串。在这将用于从任意数量的用户正在聊天的任何聊天日志中删除任何用户的消息。在
相关问题 更多 >
编程相关推荐