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
  replace or remove text from many text files Curbie 21 3,116 Jul-07-2025, 02:05 PM
Last Post: Curbie
  Unequal values when added with 0? Azdaghost 1 748 May-24-2025, 12:24 AM
Last Post: deanhystad
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 1,614 May-01-2025, 03:43 PM
Last Post: Winfried
  Replace values in Yaml file with value in dictionary PelleH 1 3,689 Feb-11-2025, 09:51 AM
Last Post: alexjordan
Exclamation Replace dando erro supergbr 1 759 Jan-28-2025, 01:27 AM
Last Post: supergbr
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 4,283 Oct-28-2024, 03:18 AM
Last Post: hobbycoder
Question [SOLVED] How to replace characters in a string? Winfried 2 1,914 Sep-04-2024, 01:41 PM
Last Post: Winfried
  replace text in a txt cartonics 19 6,848 Jan-30-2024, 06:58 AM
Last Post: Athi
  Regex replace in SQLite3 database WJSwan 1 1,912 Dec-04-2023, 05:55 PM
Last Post: Larz60+
  Replace a text/word in docx file using Python Devan 4 41,108 Oct-17-2023, 06:03 PM
Last Post: Devan

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.