Member Avatar for Doormail11

I am completely new at Python and I am not sure where to start with this task.
Basically, I want to search for a word in each line; if the word is there, output the entire line to another file (Excel or Text).

The lines look like this:
PA,IAV protein,AC0T9,DHP of IAV,2znl-A+z327-A
PPM1B,IHP of IAV,MAGEA2B,DHP of IAV,2p8e-A+z097-a

So, if the line contains IAV protein, I would like to copy the whole first line.

I don't need someone to just give me a code that works (although I won't refuse it...) but if you could point me in the right direction, that would be much appreciated.

Thanks

Recommended Answers

All 3 Replies

You must tell more clearly what would be expected output. There is also three kinds of IAV bits of info on lines:
"DHP of IAV"
"IHP of IAV" and
"IAV"

>>> if 'IAV' in "PA,IAV protein,AC0T9,DHP of IAV,2znl-A+z327-A":
    print('IAV in line')


IAV in line
>>> 
Member Avatar for Doormail11

I would like to output the entire line that contains that phrase.
So if I had the two lines
PA,IAV protein,AC0T9,DHP of IAV,2znl-A+z327-A
PPM1B,IHP of IAV,MAGEA2B,DHP of IAV,2p8e-A+z097-a

I would want to output
PA,IAV protein,AC0T9,DHP of IAV,2znl-A+z327-A
into a text file since it contains "IAV protein"
It's gotta be the whole phrase "IAV protein" rather than just "IAV"

Also, since I am looking through an entire text file, how would I search the whole file?

something like

with open('data.txt') as datain, open('dataout.txt', 'w') as dataout:
    for d in datain:
        if 'IAV protein' in d:
            dataout.write(d)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.