gpt4 book ai didi

python - 如果在 python/bash 中包含来自另一个 .txt 文件的单词,则从文件中删除行

转载 作者:行者123 更新时间:2023-11-28 21:21:14 25 4
gpt4 key购买 nike

我正在学习python,然后遇到以下困难。我要清理的文件是一个 .csv 文件。包含必须从 .csv 文件中删除的单词的文件是 .txt.txt 文件是一个域名列表:

domain.com
domain2.com
domain3.com

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

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

如果 .txt 文件包含“domain.com”,我希望删除上面的完整行。如果一些 python 忍者可以解决这个问题,我将非常感激。(或在 bash 中?)

最佳答案

嗯,既然OP是学python的……

$ python 脚本.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 - 如果在 python/bash 中包含来自另一个 .txt 文件的单词,则从文件中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com