如果从在python / bash的另一个.txt文件包含字从文件中删除行 [英] Remove line from file if containing word from another .txt file in python/bash

查看:302
本文介绍了如果从在python / bash的另一个.txt文件包含字从文件中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习蟒蛇,然后我有以下的困难。
该文件我想成为清洁是一个.csv文件。
包含必须从.csv文件删除的话的文件是一个.TXT
.txt文件是域名的列表:

I'm learning python, and then i have the following difficulties. The file i want to be cleaned is an .csv file. The file that contains the words that have to be removed from the .csv file is an .txt The .txt file is a list of domain names:

domain.com
domain2.com
domain3.com

.csv文件是一个配置文件,就像这样:

The .csv file is a config file just like this:

domain.com;8;Started;C:\inetpub\wwwroot\d\domain.com;"http *:80:www.domain.com"

如果.txt文件包含domain.com我要完整的线之上被删除。
我会很感激真的如一些Python忍者可以解决这个问题。(或在bash?)

if the .txt file contains "domain.com" i want the complete line above to be removed. I would be realy gratefully if some python ninja could fix this.(or in bash?)

推荐答案

那么,既然OP是学习Python的...

Well, since OP is learning python ...

$蟒蛇SCRIPT.py

TXT_file = 'TXT.txt'
CSV_file = 'CSV.csv'
OUT_file = 'OUTPUT.csv'

## From the TXT, create a list of domains you do not want to include in output
with open(TXT_file, 'r') as txt:
    domain_to_be_removed_list = []

    ## for each domain in the TXT
    ## remove the return character at the end of line
    ## and add the domain to list domains-to-be-removed list
    for domain in txt:
        domain = domain.rstrip()
        domain_to_be_removed_list.append(domain)


with open(OUT_file, 'w') as outfile:
    with open(CSV_file, 'r') as csv:

        ## for each line in csv
        ## extract the csv domain
        for line in csv:
            csv_domain = line.split(';')[0]

            ## if csv domain is not in domains-to-be-removed list,
            ## then write that to outfile
            if (not csv_domain in domain_to_be_removed_list):
                outfile.write(line)

这篇关于如果从在python / bash的另一个.txt文件包含字从文件中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆