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 text in a txt cartonics 19 2,072 Jan-30-2024, 06:58 AM
Last Post: Athi
  Regex replace in SQLite3 database WJSwan 1 747 Dec-04-2023, 05:55 PM
Last Post: Larz60+
  Replace a text/word in docx file using Python Devan 4 2,843 Oct-17-2023, 06:03 PM
Last Post: Devan
  Python beginner that needs an expression added to existing script markham 1 664 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  Need to replace a string with a file (HTML file) tester_V 1 698 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  why doesn't it replace all html tags? Melcu54 3 690 Jul-05-2023, 04:47 AM
Last Post: Melcu54
  Replace string in a nested Dictianory. SpongeB0B 2 1,147 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,492 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Replace with upper(string) WJSwan 7 1,545 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  Replace columns indexes reading a XSLX file Larry1888 2 951 Nov-18-2022, 10:16 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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