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)
它有效,但看起来很罗嗦.
这是写出来的合适方式,还是我错过了一个更简单的过程?
试试这个:
with open('dictionary.txt', 'r') as f:
dictionary_words = f.read().splitlines()
Run Code Online (Sandbox Code Playgroud)