<p>我有这个文本文件:</p>
<blockquote>
<p>hello world</p>
<p>world hello world</p>
</blockquote>
<p>这是我的密码:</p>
<pre><code>f = open("dummy.txt","r+")
x = 0
for line in f:
if "world" in line:
x = x + 1
print x
f.close()
</code></pre>
<p>但它打印的是<code>2</code>,而不是<code>3</code>。你知道吗</p>
<pre><code>F = open("Dummy.txt", "r+")
string = F.read() # For storing the file as a string
print string.count("world") # Print the count of "world" in the file
F.close()
</code></pre>