我希望能够从文本文件中剥离\ n字符(.rstrip(“\ n”))(词典.txt)包含120,000多个单词。然后对每行进行计数并返回txt文件中的单词数量(每个单词都在自己的行上)。 然后最后想要将所有单词存储到列表中。Python的 - 使用.readlines()与.rstrip(),然后所有的字存储到一个列表
此刻,下面的代码返回线的量,但不剥离\ n字符,因此它可以被存储到列表中。
def lines_count():
with open('dictionary.txt') as file:
print (len(file.readlines()))
可能重复的[如何逐行读取文件到列表中?](http://stackoverflow.com/questions/3277503/how-do- I-读一个文件行由行 - 进入 - 人ist) – Aprillion
您可能想要使用'str.splitlines()',在这种情况下,您可以使用'file.read()'作为字符串读取文件,然后在整个字符串中使用'splitlines()'。 – Kasramvd
请参阅[stackoverflow.com/questions/13259288/returning-a-list-of-words-after-reading-a-file-in-python](http://stackoverflow.com/questions/13259288/returning-a-在python中读取文件后的单词列表) – Ahmad