擅长:python、mysql、java
<p>是的,有办法。在</p>
<p>可以使用内置函数split将从文件读取到的字符串拆分为数组。从这里你可以找到你要找的单词的索引,然后用这个索引加一得到后面的单词。例如,使用名为测试文本看起来是这样(格式有点奇怪,因为so似乎不喜欢硬标签)。在</p>
<pre><code>one two three four five six seven eight nine
</code></pre>
<p>以下代码</p>
^{pr2}$
<p>将返回所需的“8”</p>
<p>编辑:返回列表中的每个单词</p>
<pre><code>f = open('test.txt','r')
string = f.read()
words = string.split('\t')
desired = [words[ind+1] for ind, word in enumerate(words) if word == "seven"]
</code></pre>
<p>这是使用列表理解。它枚举单词列表,如果该单词是您要查找的,则将该单词包含在列表的下一个索引处。在</p>
<p>编辑2:要在新行和制表符上拆分它,可以使用正则表达式</p>
<pre><code>import re
f = open('testtest.txt','r')
string = f.read()
words = re.split('\t|\n',string)
desired = [words[ind+1] for ind, word in enumerate(words) if word == "seven"]
</code></pre>