<p>我正在尝试使用<code>str.find()</code>在两个关键字之间提取文本。但找不到'\n'</p>
<pre><code>text = 'Cardiff, the 6th November 2007\n company \n'
String_to_extract = '6th November 2007'
keywords = {'date': ['Cardiff, the ' , '\n']}
</code></pre>
<p>代码:</p>
<pre><code>text2=text[text.find(keywords['date']0])+len(keywords[0]):text.find(keywords['date'][1])]
print(text2)
str.find() is unable to search for '\n', which results in no output
</code></pre>
<p>PS只想使用<code>str.find()</code>方法</p>
<p>概括答案。使用此代码:</p>
<pre><code>text2 = text[text.find(keywords[key][0])+len(keywords[key][0]):text.find(keywords[key][1])] # you can replace the key with whatever you want as keys
</code></pre>