R
You can use the method https://docs.python.org/3/library/stdtypes.html#str.replace but passing '\r\n' at once (and not separately, as you did):texto = 'Let the bird of loudest lay\r\nOn the sole Arabian tree\r\nHerald sad and trumpet be,\r\nTo whose sound chaste wings obey.\r\n\r\nBut thou shrieking harbinger,\r\nFoul precurrer of the fiend,\r\nAugur of the fever\'s end,\r\nTo this troop come thou not near.\r\n\r\nFrom this session interdict\r\nEvery fowl of tyrant wing,\r\nSave the eagle, feather\'d king;\r\nKeep the obsequy so strict.\r\n\r\nLet the priest in surplice white,\r\nThat defunctive music can,\r\nBe the death-divining swan,\r\nLest the requiem lack his right.\r\n\r\nAnd thou treble-dated crow,\r\nThat thy sable gender mak\'st\r\nWith the breath thou giv\'st and tak\'st,\r\n\'Mongst our mourners shalt thou go.\r\n\r\nHere the anthem doth commence:\r\nLove and constancy is dead;\r\nPhoenix and the Turtle fled\r\nIn a mutual flame from hence.\r\n\r\nSo they lov\'d, as love in twain\r\nHad the essence but in one;\r\nTwo distincts, division none:\r\nNumber there in love was slain.\r\n\r\nHearts remote, yet not asunder;\r\nDistance and no space was seen\r\n\'Twixt this Turtle and his queen:\r\nBut in them it were a wonder.\r\n\r\nSo between them love did shine\r\nThat the Turtle saw his right\r\nFlaming in the Phoenix\' sight:\r\nEither was the other\'s mine.\r\n\r\nProperty was thus appalled\r\nThat the self was not the same;\r\nSingle nature\'s double name\r\nNeither two nor one was called.'
novo_texto = texto.replace('\r\n', '')
print(novo_texto)
In the case, it exchanges all occurrences of '\r\n' by '' (the empty string), i.e. the result is the original string with all \r\n removed.Another alternative is to use regular expressions through https://docs.python.org/3/library/re.html and its method https://docs.python.org/3/library/re.html#re.Pattern.sub :import re
texto = 'Let the bird of loudest lay\r\nOn the sole Arabian tree\r\nHerald sad and trumpet be,\r\nTo whose sound chaste wings obey.\r\n\r\nBut thou shrieking harbinger,\r\nFoul precurrer of the fiend,\r\nAugur of the fever's end,\r\nTo this troop come thou not near.\r\n\r\nFrom this session interdict\r\nEvery fowl of tyrant wing,\r\nSave the eagle, feather'd king;\r\nKeep the obsequy so strict.\r\n\r\nLet the priest in surplice white,\r\nThat defunctive music can,\r\nBe the death-divining swan,\r\nLest the requiem lack his right.\r\n\r\nAnd thou treble-dated crow,\r\nThat thy sable gender mak'st\r\nWith the breath thou giv'st and tak'st,\r\n'Mongst our mourners shalt thou go.\r\n\r\nHere the anthem doth commence:\r\nLove and constancy is dead;\r\nPhoenix and the Turtle fled\r\nIn a mutual flame from hence.\r\n\r\nSo they lov'd, as love in twain\r\nHad the essence but in one;\r\nTwo distincts, division none:\r\nNumber there in love was slain.\r\n\r\nHearts remote, yet not asunder;\r\nDistance and no space was seen\r\n'Twixt this Turtle and his queen:\r\nBut in them it were a wonder.\r\n\r\nSo between them love did shine\r\nThat the Turtle saw his right\r\nFlaming in the Phoenix' sight:\r\nEither was the other's mine.\r\n\r\nProperty was thus appalled\r\nThat the self was not the same;\r\nSingle nature's double name\r\nNeither two nor one was called.'
r = re.compile(r'\r\n')
novo_texto = r.sub('', texto)
print(novo_texto)
The result is the same: all occurrences of \r\n are removed from the string.In your code you are changing the characters for a space (" "). If that's what you want, you can use texto.replace('\r\n', ' ') or r.sub(' ', texto) (repare that there is now a space between the quotation marks). Remembering that in this case, the sequence \r\n (i.e. these two characters, whenever they appear exactly in this order) will be replaced by a single space.Just to explain why your code didn't work:remove = "\r\n"
for i in range(0, len(remove)):
new_poetry = poetry[0].replace(remove[i], " ")
This for calls the method replace once to \r and again \n.
But the problem is replace returns Other string, leaving the original (poetry[0]) unchanged.Then in the first iteration you exchange all \r by space and puts in new_poetry, and in the second iteration you exchange the \n by space, but the replace is made in the original string (poetry[0]), which still contains the \r (then new_poetry now you will \n replaced, but the \r not - the previous value, obtained in the first iteration, which had only the \r replaced, is overwritten in the second iteration).Already if you use \r\n, according to the above solutions, replacement will occur only if you have \r followed by \n (and both will be replaced at once by a single space if you use ' ' in replacement methods - or removed, if you use '').The above solutions only replace one \r\n (i.e. only these two characters in this order). But if you want to replace also one \r or \n isolatedly, it can change to:import re
texto = 'Let the bird of loudest lay\r\nOn the sole Arabian tree\rHerald sad and trumpet be,\nTo whose sound chaste wings obey.'
r = re.compile(r'\r\n?|\n')
novo_texto = r.sub(' ', texto)
print(novo_texto)
Now the regex has https://www.regular-expressions.info/alternation.html (the character |, which means or), with two options:\r\n?: one \r followed by a \n optional ? makes it \n https://www.regular-expressions.info/optional.html ), or\n: the character itself \nThus, regex seeks for \r\n, or only \r (for the \n after it is optional), or (|) by only one \n. Any of these options is replaced by the string you pass on sub - in the example above, I used a space (' ').