How to break line after a specific line? PYTHON

Asked

Viewed 49 times

-4

My first question here, guys,

I need to add a line break before he writes for1 again, as I do in this code?

And I would also like for7 to be the last to be printed, as in the original file it comes by second it is printed under for1, and not last.

in_file = "nomes2.txt"
out_file = "remetentes.txt"


for1 = "nome"
for2 = "endereco"
for3 = "numero"
for4 = "bairro"
for5 = "cidade"
for6 = "cep"
for7 = "cpf"



with open(out_file, "w") as out_f:
    with open(in_file, "r") as in_f:
        for line in in_f:
            if for1 in line or for2 in line or for3 in line or for4 in line or for5 in line or for6  in line or for7 in line:
                print(line)
                out_f.write(line.replace('"', '').replace('nome:', '').replace(',',''))
```

  • Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of it. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English (short version). Do our [tour]

1 answer

-3


I believe if you add one print("\n"), is enough under the out_file

  • this adds a line break to each line, and I need the line break to be done only with each new reading of the variables, ie: for1, for2, for3, for4 , for5, for6, for7 ai the line break and then again this sequence

  • sorry, I made a mistake, if you do an if inside the if line saying that when the for1 is online, you print the print(" n")

  • Thank you very much brother, I managed to add an if in the line, ai in the print he created the break, your answer cleared legal here!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.