<分区>
从一个文本文件lines_of_words.txt
,说
first
second
third
必须创建一个单词列表作为字符串,即
list_of_strings = ['first', 'second', 'third']
这似乎是一个极其琐碎的功能,但我找不到简洁的解决方案。我的尝试要么过于繁琐,要么产生错误的输出,例如
['f', 'i', 'r', 's', 't', '\n', 's', 'e', 'c', 'o', 'n', 'd', '\n', 't', 'h', 'i', 'r', 'd', '\n']
或
first
second
third
完成这项工作最符合 Python 风格的函数是什么?到目前为止,我的出发点是
with open('list_of_words', 'r') as list_of_words:
# Do something...
print(list_of_strings)