为了账号安全,请及时绑定邮箱和手机立即绑定

提取包含关键字的行

提取包含关键字的行

慕桂英546537 2021-09-14 20:44:32
修改问题,每当col1有关键字时,我想提取col2中的时间戳关键字=[“我可以帮助你”,“我肯定可以帮助你”,“我可以检查并帮助你”,“我会非常乐意帮助你”,“让我帮助你在这方面”,“更好地帮助您”]鉴于excel数据是,    col1                                                                                                                            1.agent enters(as arrin)2.3.I'll be happy to assist you. Give me a moment to review your request.4.I see that the light in your Modem is Blinking Red. Am I right ?5.Thank you for the detailed information.6.Please do not worry.7.Don't worry johny. I can help you with that.8.Let me connect this chat to the concern team to help you out with this,   Please stay connected.   col21. 2018-10-14 21:16:582. 2018-10-14 21:17:003. 2018-10-14 21:17:404. 2018-10-14 21:18:255. 2018-10-14 21:19:396. 2018-10-14 21:19:437. 2018-10-14 21:21:048. 2018-10-14 21:22:00例如关键字之一出现在第 7 行,因此要提取 col2 中的相应时间戳所需的输出应如下所示,[出]:2018-10-14 21:21:04提前致谢
查看完整描述

2 回答

?
白板的微信

TA贡献1883条经验 获得超3个赞

尝试这个:


keywords = [

            "i can help you with that",

            "i can surely help you with that",

            "i can check and help you with that",

            "i will be more than happy to help you",

            "let me assist you on this",

            "to assist you better"

]


for phrase in keywords:

    for row in col1:

        if phrase in row.lower():

            return row

所以这是在做的是查看你的 excel 表的列......


          col1

1 Hello and welcome

2 There's a lot to see here

3 Sorry, no can do

4 I can help you with that if you'd like

并一一浏览它们。如果其中一行包含您的关键短语...


>I can help you with that< if you'd like

它会返回整行。您可以打印而不是返回或任何您想要对行执行的操作。.lower() 方法是因为我们的关键字以小写形式存储,因此应该将它们与行的小写版本进行比较。如果匹配,我们可以返回原始情况下的行。当然,我假设您已经成功地将您的数据导入 col1 中,就像某种列表一样……如果您需要帮助,请告诉我。


查看完整回答
反对 回复 2021-09-14
?
catspeake

TA贡献1111条经验 获得超0个赞

我认为这会有所帮助


import re


keywords=[

    "i can help you with that", 

    "i can surely help you with that", 

    "i can check and help you with that", 

    "i will be more than happy to help you", 

    "let me assist you on this", "to assist you better",

]


file_contents = '' # here is where you get contents from excel file


for line in file_contents:

    for keyword in keywords:

        temp = re.search(r''+ keyword +'', line, flags=re.IGNORECASE)

        if temp:

            print('[out]:',  line)


查看完整回答
反对 回复 2021-09-14
  • 2 回答
  • 0 关注
  • 177 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信