如何修复 python 不写入文件
How to fix python not writing to file
我正在制作一个脚本来读取字典并挑选出符合搜索条件的单词。代码运行良好,但问题是它没有将任何单词写入文件 "wow" 或将它们打印出来。字典的来源是 https://github.com/dwyl/english-words/blob/master/words.zip.
我尝试将文件的开头更改为 "w+" 而不是 "a+",但没有任何区别。我检查了是否没有任何词符合标准,但这不是问题所在。
listExample = [] #creates a list
with open("words.txt") as f: #opens the "words" text file
for line in f:
listExample.append(line)
x = 0
file = open("wow.txt","a+") #opens "wow" so I can save the right words to it
while True:
if x < 5000: # limits the search because I don't want to wait too long
if len(listExample[x]) == 11: #this loop iterates through all words
word = listExample[x] #if the words is 11 letters long
lastLetter = word[10]
print(x)
if lastLetter == "t": #and the last letter is t
file.write(word) #it writes the word to the file "wow"
print("This word is cool!",word) #and prints it
else:
print(word) #or it just prints it
x += 1 #iteration
else:
file.close()
break #breaks after 5000 to keep it short
它创建了 "wow" 文件,但它是空的。我该如何解决这个问题?
这解决了您的问题。您以这样的方式拆分文本,每个单词的末尾都有一个换行符,也许还有一个 space。我已经输入 .strip()
以摆脱任何白色 space。此外,我将 lastLetter
定义为 word[-1]
以获得最后一个字母,而不管单词的长度如何。
P.S。感谢 Ocaso Protal 建议剥离而不是替换。
listExample = [] #creates a list
with open("words.txt") as f: #opens the "words" text file
for line in f:
listExample.append(line)
x = 0
file = open("wow.txt","a+") #opens "wow" so I can save the right words to it
while True:
if x < 5000: # limits the search because I don't want to wait too long
word = listExample[x].strip()
if len(word) == 11:
lastLetter = word[-1]
print(x)
if lastLetter == "t": #and the last letter is t
file.write(word + '\n') #it writes the word to the file "wow"
print("This word is cool!",word) #and prints it
else:
print(word) #or it just prints it
x += 1 #iteration
else:
print('closing')
file.close()
break #breaks after 5000 to keep it short
我正在制作一个脚本来读取字典并挑选出符合搜索条件的单词。代码运行良好,但问题是它没有将任何单词写入文件 "wow" 或将它们打印出来。字典的来源是 https://github.com/dwyl/english-words/blob/master/words.zip.
我尝试将文件的开头更改为 "w+" 而不是 "a+",但没有任何区别。我检查了是否没有任何词符合标准,但这不是问题所在。
listExample = [] #creates a list
with open("words.txt") as f: #opens the "words" text file
for line in f:
listExample.append(line)
x = 0
file = open("wow.txt","a+") #opens "wow" so I can save the right words to it
while True:
if x < 5000: # limits the search because I don't want to wait too long
if len(listExample[x]) == 11: #this loop iterates through all words
word = listExample[x] #if the words is 11 letters long
lastLetter = word[10]
print(x)
if lastLetter == "t": #and the last letter is t
file.write(word) #it writes the word to the file "wow"
print("This word is cool!",word) #and prints it
else:
print(word) #or it just prints it
x += 1 #iteration
else:
file.close()
break #breaks after 5000 to keep it short
它创建了 "wow" 文件,但它是空的。我该如何解决这个问题?
这解决了您的问题。您以这样的方式拆分文本,每个单词的末尾都有一个换行符,也许还有一个 space。我已经输入 .strip()
以摆脱任何白色 space。此外,我将 lastLetter
定义为 word[-1]
以获得最后一个字母,而不管单词的长度如何。
P.S。感谢 Ocaso Protal 建议剥离而不是替换。
listExample = [] #creates a list
with open("words.txt") as f: #opens the "words" text file
for line in f:
listExample.append(line)
x = 0
file = open("wow.txt","a+") #opens "wow" so I can save the right words to it
while True:
if x < 5000: # limits the search because I don't want to wait too long
word = listExample[x].strip()
if len(word) == 11:
lastLetter = word[-1]
print(x)
if lastLetter == "t": #and the last letter is t
file.write(word + '\n') #it writes the word to the file "wow"
print("This word is cool!",word) #and prints it
else:
print(word) #or it just prints it
x += 1 #iteration
else:
print('closing')
file.close()
break #breaks after 5000 to keep it short