Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all newline characters using python

I am trying to read a pdf using python and the content has many newline (crlf) characters. I tried removing them using below code:

from tika import parser

filename = 'myfile.pdf'
raw = parser.from_file(filename)
content = raw['content']
content = content.replace("\r\n", "")
print(content)

But the output remains unchanged. I tried using double backslashes also which didn't fix the issue. can someone please advise?

like image 233
Leni Avatar asked Mar 21 '26 23:03

Leni


1 Answers

content = content.replace("\\r\\n", "")

You need to double escape them.