从文本文件中的行创建字符串列表的最Pythonic方法?

5 python string list text-files

从文本文件中lines_of_words.txt

第一
第二
第三

必须创建一个单词列表作为字符串,即

list_of_strings = ['first', 'second', 'third']
Run Code Online (Sandbox Code Playgroud)

这看起来是一个极其微不足道的功能,但我找不到简洁的解决方案。我的尝试要么太麻烦,要么产生错误的输出,例如

['f', 'i', 'r', 's', 't', '\n', 's', 'e', 'c', 'o', 'n', 'd', '\n', 't', 'h', 'i', 'r', 'd', '\n']
Run Code Online (Sandbox Code Playgroud)

或者

first
second
third
Run Code Online (Sandbox Code Playgroud)

完成这个任务最Pythonic的函数是什么?到目前为止我的出发点是

with open('list_of_words', 'r') as list_of_words:
    # Do something...
print(list_of_strings)
Run Code Online (Sandbox Code Playgroud)

Wil*_*sem 4

list(..)您只需在文件处理程序上使用即可。由于字符串将包含一个新行,因此您可能需要使用str.rstrip删除'\n'右侧的部分:

with open('list_of_words', 'r') as f:
    list_of_strings = list(map(str.rstrip, f))
print(list_of_strings)
Run Code Online (Sandbox Code Playgroud)