猿问

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

我正在尝试获取文件作为输入并搜索特殊字符。我将 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' ' .' ]


江户川乱折腾
浏览 126回答 2
2回答
随时随地看视频慕课网APP

相关分类

Python
我要回答