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

如何在文件中搜索特定的 from 和 to 关键字并在 python 中打印句子

如何在文件中搜索特定的 from 和 to 关键字并在 python 中打印句子

江户川乱折腾 2021-06-16 13:15:10
我正在尝试获取文件作为输入并搜索特殊字符。我将 from 和 to 键作为输入。如果 to 关键字在下一行,我应该打印直到找到 to 关键字。for line in contents:if line.startswith("*CHI: ") :       line = line.strip("*")       tokenize = line.split()       p.token_filter(tokenize)说我有一个文件:       *CHI: (hi) new [/] friend [//] there [/-] [ bch] [ bch ] /[]/ [/=]<new><there>.%mod: hi there.*CHI: <dude><there>*CHI: &=sighs <and instead the> [//] and then the gira?e got it and gave it to the elephant . *CHI: <he> [/] <he> [/] he hold it .*CHI: then [/-] the doctor give the [/] money to the man*CHI: and (i)s then (.) the little (.) gira?e is crying because it (i)s sinking通过使用上面的代码,我得到如下输出:['new', '[/]', 'friend', '[//]', 'there', 'bch', '/[]/']['dude', 'dude']['and', 'and', 'instead', 'the', 'the', '[//]', 'and', 'then', 'the', 'gira?e', 'got', 'it', 'and', 'gave', 'it', 'to', 'the', 'elephant', '.']['he', 'he', '[/]', 'he', 'he', '[/]', 'he', 'hold', 'it', '.']['then', 'the', 'doctor', 'give', 'the', '[/]', 'money', 'to', 'the', 'man']['and', 'then', '(.)', 'the', 'little', '(.)', 'gira?e', 'is', 'crying', 'because', 'it', 'sinking']我的另一个目标是我应该打印 ['new', '[/]', 'friend', '[//]', 'there', 'bch', '/[]/' 'new' 'there' ' .' ]
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 125 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号