I have developed a code to "capture" in several input.txt files those lines with a especific query, but now I would like to find several queries. For instance "find: AA or CD or BC" in the input file.
I have not found any way to do so. Any suggestion would be so helpful!
Input file
AA
AB
AC
CD
CD
AA
BC
Output file
AA
CD
CD
AA
BC
This is the code I am using now:
import fileinput
from collections import deque
output_file = 'output.txt'
buscado = 'AA'
contexto = deque([], 4) # for keeping the last 4 lines
with open(output_file, "w") as f_out:
for line in fileinput.input(files=["input.txt", "input2.txt"]):
contexto.append(line)
if len(contexto) < 4:
continue
if buscado in contexto[1]:
f_out.writelines(contexto)
You can do like this:
output_list = []
with open('data.txt', 'rt') as f:
data = f.readlines()
for line in data:
if line.strip('\n') in ("AA", "CD", "BC"):
output_list.append(line.strip('\n'))
print(output_list)
Output:
['AA', 'CD', 'CD', 'AA', 'BC']
[EDIT] Using fileinput
If you want to use fileinput module to parse multiple files and to know in each file what keys are found it you can try like this:
import fileinput
with fileinput.FileInput(files=('data.txt', 'data1.txt'), mode='r') as input:
for idx, line in enumerate(input):
if input.isfirstline() == True:
# Indicate the file name
print(f'Reading file : {input.filename()}')
if line.strip('\n') in ("AA", "CD", "BC"):
print(line.strip('\n'))
Content of data.txt
AA
AB
AC
CD
CD
AA
BC
and
Content of data1.txt
AA
AB
AC
CD
CD
AA
BC
AA
BB
DD
FF
BC
BC
Output:
Reading file : data.txt
AA
CD
CD
AA
BC
Reading file : data1.txt
AA
CD
CD
AA
BC
AA
BC
BC
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.