从文件读取到列表时是否有更短的方法来删除换行符?

som*_*me1 0 python file-io whitespace list strip

这是我目前的代码:

dfile = open('dictionary.txt', 'r')
sfile = open('substrings.txt', 'r')
dictionary_words = []
substrings = []

for line in dfile:
    dictionary_words.append(line.rstrip('\n'))
for line in sfile:
    substrings.append(line.rstrip('\n'))
Run Code Online (Sandbox Code Playgroud)

它有效,但看起来很罗嗦.

这是写出来的合适方式,还是我错过了一个更简单的过程?

dme*_*sky 9

试试这个:

with open('dictionary.txt', 'r') as f:
    dictionary_words = f.read().splitlines()
Run Code Online (Sandbox Code Playgroud)