[英]How do I find all strings of length 7 in a text file and then save those strings to a new text file in Python?
我有一个拼字游戏词典中所有单词的文件。 我对编码很陌生,但我想创建一个新文件,其中包含所有 7 个字母的单词并将它们复制到一个新文件中。 我怎么做?
假设您的输入文件每行一个单词,您可以读取每一行,检查单词的长度,如果是 7,则写入 output 文件:
with open('input.txt', 'r') as f_in, open('output.txt', 'w') as f_out:
for line in f_in:
if len(line.strip()) == 7: # we strip the newline and possible spaces
f_out.write(line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.