28

我正在尝试从文本文件中读取文本,读取行,删除包含特定字符串的行(在本例中为“坏”和“顽皮”)。我写的代码是这样的:

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()

我是这样写的,但没有成功。

重要的一件事是,如果文本的内容是这样的:

good baby
bad boy
good boy
normal boy

我不希望输出有空行。所以不喜欢:

good baby

good boy
normal boy

但像这样:

good baby
good boy
normal boy

我应该从上面的代码中编辑什么?

4

10 回答 10

78

您可以像这样使您的代码更简单,更具可读性

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)

使用上下文管理器任何.

于 2012-08-15T12:43:47.373 回答
7

您可以简单地不将该行包含到新文件中,而不是进行替换。

for line in infile :
     if 'bad' not in line and 'naughty' not in line:
            newopen.write(line)
于 2012-08-15T12:11:50.137 回答
7

我用它从文本文件中删除不需要的单词:

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)

或者对目录中的所有文件执行相同操作:

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)

我确信必须有一种更优雅的方式来做到这一点,但这正是我想要的。

于 2017-07-31T09:45:11.447 回答
3

今天我需要完成一个类似的任务,所以我根据我所做的一些研究写了一个完成任务的要点。我希望有人会发现这很有用!

import os

os.system('cls' if os.name == 'nt' else 'clear')

oldfile = raw_input('{*} Enter the file (with extension) you would like to strip domains from: ')
newfile = raw_input('{*} Enter the name of the file (with extension) you would like me to save: ')

emailDomains = ['windstream.net', 'mail.com', 'google.com', 'web.de', 'email', 'yandex.ru', 'ymail', 'mail.eu', 'mail.bg', 'comcast.net', 'yahoo', 'Yahoo', 'gmail', 'Gmail', 'GMAIL', 'hotmail', 'comcast', 'bellsouth.net', 'verizon.net', 'att.net', 'roadrunner.com', 'charter.net', 'mail.ru', '@live', 'icloud', '@aol', 'facebook', 'outlook', 'myspace', 'rocketmail']

print "\n[*] This script will remove records that contain the following strings: \n\n", emailDomains

raw_input("\n[!] Press any key to start...\n")

linecounter = 0

with open(oldfile) as oFile, open(newfile, 'w') as nFile:
    for line in oFile:
        if not any(domain in line for domain in emailDomains):
            nFile.write(line)
            linecounter = linecounter + 1
            print '[*] - {%s} Writing verified record to %s ---{ %s' % (linecounter, newfile, line)

print '[*] === COMPLETE === [*]'
print '[*] %s was saved' % newfile
print '[*] There are %s records in your saved file.' % linecounter

链接到 Gist:emailStripper.py

最好的,阿兹

于 2016-09-09T16:48:51.197 回答
2

else只连接到最后一个if。你想要elif

if 'bad' in line:
    pass
elif 'naughty' in line:
    pass
else:
    newopen.write(line)

另请注意,我删除了行替换,因为无论如何您都不会写这些行。

于 2012-08-15T12:15:50.610 回答
2

使用 python-textops 包:

from textops import *

'oldfile.txt' | cat() | grepv('bad') | tofile('newfile.txt')
于 2017-02-27T18:26:02.853 回答
1

正则表达式比我使用的公认答案(对于我的 23 MB 测试文件)要快一些。但是里面没有很多。

import re

bad_words = ['bad', 'naughty']

regex = f"^.*(:{'|'.join(bad_words)}).*\n"
subst = ""

with open('oldfile.txt') as oldfile:
    lines = oldfile.read()

result = re.sub(regex, subst, lines, re.MULTILINE) 

with open('newfile.txt', 'w') as newfile:
    newfile.write(result)

在此处输入图像描述

于 2020-10-16T12:23:47.480 回答
1

试试这个效果很好。

import re

text = "this is bad!"
text = re.sub(r"(.*?)bad(.*?)$|\n", "", text)
text = re.sub(r"(.*?)naughty(.*?)$|\n", "", text)
print(text)

于 2021-05-31T11:46:04.390 回答
0
to_skip = ("bad", "naughty")
out_handle = open("testout", "w")

with open("testin", "r") as handle:
    for line in handle:
        if set(line.split(" ")).intersection(to_skip):
            continue
        out_handle.write(line)
out_handle.close()
于 2012-08-15T12:27:14.247 回答
0
bad_words = ['doc:', 'strickland:','\n']

with open('linetest.txt') as oldfile, open('linetestnew.txt', 'w') as newfile:
    for line in oldfile:
        if not any(bad_word in line for bad_word in bad_words):
            newfile.write(line)

\n是换行符的 Unicode 转义序列。

于 2019-12-09T21:09:33.373 回答