回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>正在阅读文档(<a href="https://docs.python.org/2/tutorial/inputoutput.html" rel="nofollow">https://docs.python.org/2/tutorial/inputoutput.html</a>):</p>
<blockquote>
<p>For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:</p>
<p>If you want to read all the lines of a file in a list you can also use <code>list(f)</code> or <code>f.readlines()</code>.</p>
</blockquote>
<p>所以我试了一下:</p>
<pre><code>with open(file) as f:
dictionary = sorted(list(f))
#debug
print dictionary
</code></pre>
<p>结果是:</p>
<pre><code>['anuria\n', 'anus\n', 'anuses\n', 'anvil\n', 'anvils\n', 'anxiety\n',
'anxious\n ', 'any\n', 'anybody\n', 'anyhow\n', 'anymore\n',
'anyone\n', 'anyplace\n', 'any thing\n', 'anytime\n', 'anyway\n',
'anywhere\n', 'anywise\n', 'aorta\n', 'aortae \n', 'aortal\n',
'aortas\n', 'aortic\n', 'aortitis\n', 'aoudad\n', 'aoudads\n',
'apace\n', 'apache\n', 'apaches\n', 'apanage\n', 'apart\n',
'apathies\n', 'apathy'...]
</code></pre>
<p>两个问题:</p>
<ol>
<li><p>为什么<code>\n</code>换行提要存在?</p></li>
<li><p>有办法把它去掉吗?或者我必须使用<code>readline()</code>并手动追加?</p></li>
</ol>