Python Forum
Search & Replace - Newlines Added After Replace - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Search & Replace - Newlines Added After Replace (/thread-11697.html)



Search & Replace - Newlines Added After Replace - dj99 - Jul-22-2018

Hi,

the below script works, but it adds a new line after it replaces

I cant understand why
it should just do a straight replace


with open('c:/Users/Dan/Desktop/Replace.txt') as f:            
    for l in f:
        #s = l.split()
        s = l.split('|')
        editor.replace(s[0], s[1])
Replace file
apple|pear
hello|Hi


RE: Search & Replace - Newlines Added After Replace - buran - Jul-22-2018

what is editor?
don't forget that each line in the file ends with '\n' (i.e. new line). If you print the line print will also add new line. you can use optional end parameter OR strip the whitespace:
>>> line='somethin, something else\n'
>>> print(line)
somethin, something else

>>> print(line, end='')
somethin, something else
>>> print(line.strip())
somethin, something else
>>>



RE: Search & Replace - Newlines Added After Replace - dj99 - Jul-22-2018

Hello Buran,

This is to do search and replacement in Notepad++ editor
I am doing lots of search and replace

originally
s = l.split()
does not cause this issue
i tried the below
 editor.replace(s[0], s[1])
 editor.line.strip()



RE: Search & Replace - Newlines Added After Replace - buran - Jul-22-2018

if it is Python Script for Notepad++ plug-in related and there was no problem before, you may want to raise an issue on their Github https://github.com/bruderstein/PythonScript/issues