Python Forum
Search & Replace - Newlines Added After Replace
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search & Replace - Newlines Added After Replace
#1
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



:)


Python newbie trying to learn the ropes
Reply
#2
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
>>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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()



:)


Python newbie trying to learn the ropes
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 220 3 hours ago
Last Post: Winfried
  Replace values in Yaml file with value in dictionary PelleH 1 2,216 Feb-11-2025, 09:51 AM
Last Post: alexjordan
Exclamation Replace dando erro supergbr 1 381 Jan-28-2025, 01:27 AM
Last Post: supergbr
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 2,378 Oct-28-2024, 03:18 AM
Last Post: hobbycoder
Question [SOLVED] How to replace characters in a string? Winfried 2 1,083 Sep-04-2024, 01:41 PM
Last Post: Winfried
  replace text in a txt cartonics 19 5,207 Jan-30-2024, 06:58 AM
Last Post: Athi
  Regex replace in SQLite3 database WJSwan 1 1,512 Dec-04-2023, 05:55 PM
Last Post: Larz60+
  Replace a text/word in docx file using Python Devan 4 22,839 Oct-17-2023, 06:03 PM
Last Post: Devan
  Python beginner that needs an expression added to existing script markham 1 1,392 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  Need to replace a string with a file (HTML file) tester_V 1 1,963 Aug-30-2023, 03:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020