脚本 returns 需要字符串,但在其后加上 '\n'
Script returns wanted strings, but with '\n' put after it
我为脚本编写了这段代码,该脚本应检查单词是否包含所有元音字母(so、e、u、i、o、a),不区分大小写:
def words_all_vowels(filename):
"""Returns a list of all words that contain all vowels, as found in the file with the given name."""
result = []
e = False
a = False
i = False
o = False
u = False
with open(filename, 'r') as f:
for line in f:
for r in range(len(line.lower())-1):
if line.lower()[r:(r+1)] == "e":
e = True
if line.lower()[r:(r+1)] == "a":
a = True
if line.lower()[r:(r+1)] == "i":
i = True
if line.lower()[r:(r+1)] == "o":
o = True
if line.lower()[r:(r+1)] == "u":
u = True
if e == True and a == True and i == True and o == True and u == True:
print(line)
result.append(line)
#resetting for next word
e == False
a == False
i == False
o == False
u == False
return result
pass # TODO
奇怪的是,脚本 return 不仅包含在我的行变量中的字符串,而且后面还有 \n。这是包含所有元音的任意潜在单词列表的 return:
['Aboideaus\n',
'seagull\n',
'multidirectional\n',
'lifeguard\n',
'complaint\n',
'overqualified\n']
这是不正确的,因为它还包含不包含所有元音的单词,但我稍后会解决这个问题。我只想知道这个 '\n' 是从哪里来的。它看起来像是一个以某种方式包含在字符串中的中断。包含这些词的原始文档中的词用行分隔,所以也许这就是它的来源。我在另一个脚本中也有这个,所以我阅读这些行的方式一定有问题。
使用 line.strip()
在追加结果时删除换行符。
def words_all_vowels(filename):
"""Returns a list of all words that contain all vowels, as found in the file with the given name."""
result = []
e = False
a = False
i = False
o = False
u = False
with open(filename, 'r') as f:
for line in f:
for r in range(len(line.lower())-1):
if line.lower()[r:(r+1)] == "e":
e = True
if line.lower()[r:(r+1)] == "a":
a = True
if line.lower()[r:(r+1)] == "i":
i = True
if line.lower()[r:(r+1)] == "o":
o = True
if line.lower()[r:(r+1)] == "u":
u = True
if e == True and a == True and i == True and o == True and u == True:
print(line)
result.append(line.strip()) # add strip
#resetting for next word
e == False
a == False
i == False
o == False
u == False
return result
问题是当你从文件中读取时,行最后有默认的 \n 去到新行
你可以使用 line.strip()
或者你可以使用 line.replace('\n','')
我为脚本编写了这段代码,该脚本应检查单词是否包含所有元音字母(so、e、u、i、o、a),不区分大小写:
def words_all_vowels(filename):
"""Returns a list of all words that contain all vowels, as found in the file with the given name."""
result = []
e = False
a = False
i = False
o = False
u = False
with open(filename, 'r') as f:
for line in f:
for r in range(len(line.lower())-1):
if line.lower()[r:(r+1)] == "e":
e = True
if line.lower()[r:(r+1)] == "a":
a = True
if line.lower()[r:(r+1)] == "i":
i = True
if line.lower()[r:(r+1)] == "o":
o = True
if line.lower()[r:(r+1)] == "u":
u = True
if e == True and a == True and i == True and o == True and u == True:
print(line)
result.append(line)
#resetting for next word
e == False
a == False
i == False
o == False
u == False
return result
pass # TODO
奇怪的是,脚本 return 不仅包含在我的行变量中的字符串,而且后面还有 \n。这是包含所有元音的任意潜在单词列表的 return:
['Aboideaus\n',
'seagull\n',
'multidirectional\n',
'lifeguard\n',
'complaint\n',
'overqualified\n']
这是不正确的,因为它还包含不包含所有元音的单词,但我稍后会解决这个问题。我只想知道这个 '\n' 是从哪里来的。它看起来像是一个以某种方式包含在字符串中的中断。包含这些词的原始文档中的词用行分隔,所以也许这就是它的来源。我在另一个脚本中也有这个,所以我阅读这些行的方式一定有问题。
使用 line.strip()
在追加结果时删除换行符。
def words_all_vowels(filename):
"""Returns a list of all words that contain all vowels, as found in the file with the given name."""
result = []
e = False
a = False
i = False
o = False
u = False
with open(filename, 'r') as f:
for line in f:
for r in range(len(line.lower())-1):
if line.lower()[r:(r+1)] == "e":
e = True
if line.lower()[r:(r+1)] == "a":
a = True
if line.lower()[r:(r+1)] == "i":
i = True
if line.lower()[r:(r+1)] == "o":
o = True
if line.lower()[r:(r+1)] == "u":
u = True
if e == True and a == True and i == True and o == True and u == True:
print(line)
result.append(line.strip()) # add strip
#resetting for next word
e == False
a == False
i == False
o == False
u == False
return result
问题是当你从文件中读取时,行最后有默认的 \n 去到新行
你可以使用 line.strip()
或者你可以使用 line.replace('\n','')