0

I have this text file:

>P1;3RVYA

sequence::     : :     : :::-1.00:-1.00
MYLRITNIVESSFFTKFIIYLIVLNGITMGLETSKTFMQSFGVYTTLFNQIVITIFTIEIILR-IYVHRISFFKD
PWSLFDFFVVAISLVPTSS---GFEILRVLRVLRLFRLVTAVPQMRKIVSALISVIPGMLSVIALMTLFFYIFAI
MATQLFGERFP---------------------------------------------EWFGTLGESFYTLFQVMTL
ESWSMGIVRP-LMEVYPYAWVFFIPFIFVVTFVMINLVVAICVDAM*

>P1;Dominio1

sequence::     : :     : :::-1.00:-1.00
EWPPFEYMILATIIANCIVLALEQH---LPDDDKTPMSERLDDTEPYFIGIFCFEAGIKIIALGFAFHKGSYLRN
GWNVMDFVVVLTGILATVGTEFDLRTLRAVRVLRPLKLVSGIPSLQVVLKSIMKAMIPLLQIGLLLFFAILIFAI
IGLEFYMGKFHTTCFEEGTDDIQGESPAPCGTEEPARTCPNGTKCQPYWEGPNNGITQFDNILFAVLTVFQCITM
EGWTDLLYNSNDASGNTWNWLYFIPLIIIGSFFMLNLVLGVLSGEF*

I need to replace the word "sequence", it is under of the word 3RVYA (only that)

I have this command:

a="3RVYA"
for line in file('%s'%I):
        if a in line:
            print line

But just printed "3RV", I need only print the next line, that have the word "sequence", I need it to replace "sequence" for "structure".

I'm beginner in python, so...Can somebody help me please? Thanks so much

5
  • 4
    Your title says "read lines", your text says "replace", and what your code does is print... Unclear :/ Commented Jan 16, 2015 at 16:05
  • I have a text file, and I need the next instructions : 1.- search '3RVYA" 2.- In the next line (the line that is under the line that has 3RVYA), search the word "sequence" and replace it for "structure". The command that I have, only search the word '3RVYA' in the file and print it, but I dont know how to do for to replace word of the next line. Commented Jan 16, 2015 at 16:11
  • docs.python.org/2/library/re.html can be handy! Commented Jan 16, 2015 at 16:11
  • Don't use regular expressions if it is always the first word on its line. Just search every line of the text file and see if it starts with that word. Commented Jan 16, 2015 at 16:21
  • If you always have the same two newlines between the domain name and the word "sequence", then if you can just read the whole file and save it to a string, you could just print all_the_text.replace('3RVYA\n\nsequence', '3RVYA\n\nstructure') Commented Jan 16, 2015 at 16:27

1 Answer 1

1

You appear to be trying to modify a text file in-place by selectively changing some parts of it with other strings of different length. This isn't really possible, because of the way filesystems work nowadays (that is, byte-by-byte). However, the stdlib module fileinput simulates it well (behind the curtains it writes a new file, then at end end atomically replaces the old file with the new one). So...:

import fileinput
replacing = False
for line in fileinput.input('thefile.txt', inplace=True):
    if replacing and 'sequence' in line:
        line = line.replace('sequence', 'structure')
        replacing = False
    elif '3RVYA' in line:
        replacing = True
    print line,

This is Python 2; in Python 3, the last line becomes, instead:

    print(line, end='')
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks so much, but, with this commands, I only have a file with >3RVYA I need something like: before: >P1;3RVYA sequence:: : : : :::-1.00:-1.00 MYLRITNIVESSFFTKFIIYLIVLNG* >P1;Dominio1 sequence:: : : : :::-1.00:-1.00 EWPPFEYMILATIIANCIVLALEQHL* after: >P1;3RVYA structure:: : : : :::-1.00:-1.00 MYLRITNIVESSFFTKFIIYLIVLNGI* >P1;Dominio1 sequence:: : : : :::-1.00:-1.00 WEGPNNGITQFDNILFAVLTVFQCITM*
@TereHs, with the indents I used (reproduce them carefully!) the file's contents are not changed except for that selective replacement. If you see only that one line you've probably messed up the indenting, specifically by indenting the print four spaces more than it should be (use spaces, not tabs).
@TereHs, copy and paste my code with its exact indenting and run it. Not sure what is it you "read read read" but the code snippet above works.
thanks so much, I had changed something in the code, and it was wrong. But it's right. Thanks so much!!!! My problem is resolved. Thanks. Have a nice day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.