擅长:python、mysql、java
<p>使用<a href="http://docs.python.org/library/random.html#random.choice" rel="nofollow">random.choice</a>:</p>
<pre><code>import random
def random_word(filename):
with open(filename) as file:
return random.choice(file.readlines())
filenames = ["file.txt", "another.txt"]
print("Thou " + ' '.join(map(random_word, filenames)))
</code></pre>
<p>如果需要多次获取行,也可以使用<a href="http://docs.python.org/library/linecache.html" rel="nofollow">^{<cd1>}</a>。在</p>