从文件中读取搜索字符串并为另一个fi中的字段添加字符串

2024-06-16 13:20:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要读2个文件,比如file1和file2

文件1有搜索字符串

文件2有一些数据

要求从文件1中逐行读取搜索字符串,并根据文件2中的行搜索该字符串,如果找到,则在文件2中为2字段添加“\u done”。你知道吗

示例: 文件1:有

BEN2T
KEN3T
MILDRED

文件2:有

RICKy2 Monthly    "CASE, WORKLOAD, INVENTORY" Workload-cli
BEN2T Monthly   INTERFACES      Interface-cli
KEN3T Daily   INTERFACES      Interface-cli
MARCUS3 Monthly   "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY"    Workload-cli
NANCY2 Monthly   "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY"    Workload-cli
MILDRED Monthly "FISCAL, CLAIMS"    Port

现在预计输出.txt如下所示:

RICKy2 Monthly    "CASE, WORKLOAD, INVENTORY" Workload-cli
BEN2T Monthly_done   INTERFACES      Interface-cli
KEN3T Daily_done   INTERFACES      Interface-cli
MARCUS3 Monthly   "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY"    Workload-cli
NANCY2 Monthly   "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY"    Workload-cli
MILDRED Monthly_done    "FISCAL, CLAIMS"    Port

已尝试:

    #!/usr/bin/env python3
import fileinput

with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(text_to_search, replacement_text), end='')

但这是替换文件。你知道吗

我可以用shell或python

谢谢


Tags: 文件字符串cliinterfacesinterfaceinventorycasedone
2条回答

您可以使用readfile1到集合中,以便使用生成器表达式查找与从csv.reader方法生成的名称匹配的项:

import csv
done = set(map(str.rstrip, open('file1')))
with open('file2_new', 'w') as f:
    csv.writer(f).writerows((name, freq + 'done', *rest) for name, freq, *rest in csv.reader(open('file2'), delimiter=' ', skipinitialspace=True) if name in done)

你能试试下面的吗。你知道吗

awk 'FNR==NR{a[tolower($0)];next} {$2=tolower($1) in a?$2"_done":$2} 1' Input_file1   Input_file2

或者根据@blhsing的评论,下面也可能会有所帮助。你知道吗

awk '                                   ##Starting awk program here.
FNR==NR{                                ##checking condition FNR==NR which will be TRUE when fir Input_file isbeing read.
  a[tolower($0)]                        ##Creating an array named a whose index is value of current line andtolower changes line to all lower characters.
  next                                  ##next will skip all further statements from here.
}
tolower($1) in a{                       ##checking if lower value of $1 is present in array a if yes then do following.
  $2=$2"_done"                          ##Appending _done to value of $2 here.
}
1
'   file1  file2                        ##Mentioning Input_file names here.

解释:立即添加代码解释。你知道吗

awk '                                   ##Starting awk program here.
FNR==NR{                                ##checking condition FNR==NR which will be TRUE when fir Input_file isbeing read.
  a[tolower($0)]                        ##Creating an array named a whose index is value of current line andtolower changes line to all lower characters.
  next                                  ##next will skip all further statements from here.
}
{                                       ##Starting a block here which will be executed once 2nd Input_file named Input_file2 is being read.
  $2=tolower($1) in a?$2"_done":$2      ##Here checking condition if lower value of $1 is present in array a if yes then append _done to$2 or keep it as it is.
}                                       ##Closing block here.
1                                       ##Mentioning 1 here will print edited/non-edited value of line.
'   Input_file1   Input_file2           ##Mentioning Input_file names here.

输出如下。你知道吗

RICKy2 Monthly "CASE, WORKLOAD, INVENTORY" Workload-cli
BEN2T Monthly_done INTERFACES Interface-cli
KEN3T Daily_done INTERFACES Interface-cli
MARCUS3 Monthly "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY" Workload-cli
NANCY2 Monthly "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY" Workload-cli
MILDRED Monthly_done "FISCAL, CLAIMS" Port

相关问题 更多 >