-1

I have a file1 which has text as below

boy men women
girl women men

I have a file2 which has text as below

good babyboy
good babygirl
bad babyboy
bad babygirl
xyz
abc

Now I want to search for the lines in file2 with first word in file1 (boy or girl) along with the word good and print out those lines in file3.

file3 should have

good babyboy
good babygirl

Can someone help me with the code.

I tried the one below

def main():
    input_file_1 = 'file1'
    input_file_2 = 'file2'
    output_file = 'file3'

    with open(input_file_1, 'r') as f:
        lines_1 = f.readlines()
    with open(input_file_2, 'r') as f:
        lines_2 = f.readlines()
    with open(output_file, 'w') as f:
        for line_1 in lines_1:
            words = line_1.split()
            for line_2 in lines_2:
                if words[0] in line_2:
                    if 'good' in line_2:
                        f.write(line_2)

I don't get any output :(

Vivek
  • 19
  • 1
  • When I try the code and **call the `main` function**, I get the expected result. Please note that the name `main` is **not special** in Python. See the linked duplicate for details. As some general advice going forward: 1) please try to make sure you understand fundamentals before writing more complex logic; 2) please read [ask] and note well that this is **not a discussion forum**; 3) please read [mre] and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and look for the cause of problems yourself first, by studying *what actually happens, step by step* when you try the code. – Karl Knechtel May 10 '23 at 01:07
  • By putting in proper debugging effort first, it becomes possible to **ask a clear question that actually reflects** the difficulty you are encountering. The problem here is, evidently, nothing to do with "searching for a word in a line"; you do know how to do that, and "even if it is part of a bigger word" doesn't change anything about how to do it. – Karl Knechtel May 10 '23 at 01:08

1 Answers1

-2

Try this code:

def main():
    input_file_1 = 'file1'
    input_file_2 = 'file2'
    output_file = 'file3'

    with open(input_file_1, 'r') as f:
        lines_1 = f.readlines()
    with open(input_file_2, 'r') as f:
        lines_2 = f.readlines()
    with open(output_file, 'w') as f:
        for line_1 in lines_1:
            words = line_1.strip().split()
            if words[0] in ['boy', 'girl']:
                for line_2 in lines_2:
                    if words[0] in line_2 and 'good' in line_2:
                        f.write(line_2)
                f.seek(0)
  • Welcome to Stack Overflow. Please read [answer]. OP had working code; the problem is simply with not calling `main`. It's important to verify things like this before trying to write an answer. Please also try to stick to questions that are properly asked. The goal on Stack Overflow is to build a searchable Q&A library, **not** to help individual users with problems - this is **not a discussion forum**. We don't want to try to "find the bug" for OP here, but instead *explain* a problem with code. For this reason, simply saying "try this code" usually also does not make a good answer. – Karl Knechtel May 10 '23 at 01:10