我正在尝试从文本文件中读取文本,读取行,删除包含特定字符串的行(在本例中为"bad"和"naughty").我写的代码是这样的:
infile = file('./oldfile.txt')
newopen = open('./newfile.txt', 'w')
for line in infile :
if 'bad' in line:
line = line.replace('.' , '')
if 'naughty' in line:
line = line.replace('.', '')
else:
newopen.write(line)
newopen.close()
Run Code Online (Sandbox Code Playgroud)
我这样写了但是没有用.
一件重要的事情是,如果文本的内容是这样的:
good baby
bad boy
good boy
normal boy
Run Code Online (Sandbox Code Playgroud)
我不希望输出有空行.所以不喜欢:
good baby
good boy
normal boy
Run Code Online (Sandbox Code Playgroud)
但像这样:
good baby
good boy
normal boy
Run Code Online (Sandbox Code Playgroud)
我应该从上面的代码中编辑什么?
slo*_*oth 55
您可以使代码更简单,更易读
bad_words = ['bad', 'naughty']
with open('oldfile.txt') as oldfile, open('newfile.txt', 'w') as newfile:
for line in oldfile:
if not any(bad_word in line for bad_word in bad_words):
newfile.write(line)
Run Code Online (Sandbox Code Playgroud)
您可以不将该行包含在新文件中而不是替换.
for line in infile :
if 'bad' not in line and 'naughty' not in line:
newopen.write(line)
Run Code Online (Sandbox Code Playgroud)
小智 5
我用它来从文本文件中删除不需要的单词:
bad_words = ['abc', 'def', 'ghi', 'jkl']
with open('List of words.txt') as badfile, open('Clean list of words.txt', 'w') as cleanfile:
for line in badfile:
clean = True
for word in bad_words:
if word in line:
clean = False
if clean == True:
cleanfile.write(line)
Run Code Online (Sandbox Code Playgroud)
或对目录中的所有文件执行相同的操作:
import os
bad_words = ['abc', 'def', 'ghi', 'jkl']
for root, dirs, files in os.walk(".", topdown = True):
for file in files:
if '.txt' in file:
with open(file) as filename, open('clean '+file, 'w') as cleanfile:
for line in filename:
clean = True
for word in bad_words:
if word in line:
clean = False
if clean == True:
cleanfile.write(line)
Run Code Online (Sandbox Code Playgroud)
我敢肯定必须有一种更优雅的方法来做到这一点,但这确实达到了我想要的目的。
| 归档时间: |
|
| 查看次数: |
80685 次 |
| 最近记录: |