<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to Stop Editor Replace (Python Script) from Inserting New Lines?]]></title><description><![CDATA[<p dir="auto">Hello everyone, it’s me again!<br />
How do I find and replace using this script without inserting new lines or retain the original format?<br />
The Script:</p>
<pre><code>with open("C:\Users\Mayonnaisu\Desktop\New folder\Test.txt") as f:
    for l in h:
        s = l.split("*")
        editor.replace(s[0], s[1])
</code></pre>
<p dir="auto">Test.txt</p>
<pre><code>let's*why
see*the
it*how
</code></pre>
<p dir="auto">The File:</p>
<pre><code>let's see it
see
</code></pre>
<p dir="auto">The Output:</p>
<pre><code>why
 the
 how
the

</code></pre>
<p dir="auto">The Desired One:</p>
<pre><code>why the how
the
</code></pre>
<p dir="auto">Could anyone help me?<br />
Thanks in advance!</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/24122/how-to-stop-editor-replace-python-script-from-inserting-new-lines</link><generator>RSS for Node</generator><lastBuildDate>Sat, 17 Oct 2026 11:31:29 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/24122.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 Feb 2023 12:24:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to Stop Editor Replace (Python Script) from Inserting New Lines? on Sun, 12 Feb 2023 05:28:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a><br />
Oh, you’re right! Thanks again!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84084</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84084</guid><dc:creator><![CDATA[Mayonnaisu]]></dc:creator><pubDate>Sun, 12 Feb 2023 05:28:54 GMT</pubDate></item><item><title><![CDATA[Reply to How to Stop Editor Replace (Python Script) from Inserting New Lines? on Sat, 11 Feb 2023 16:16:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mayonnaisu" aria-label="Profile: Mayonnaisu">@<bdi>Mayonnaisu</bdi></a> said in <a href="/post/84072">How to Stop Editor Replace (Python Script) from Inserting New Lines?</a>:</p>
<blockquote>
<p dir="auto">I was not given any error warning for such a mistake.</p>
</blockquote>
<p dir="auto">So perhaps you had it defined as <code>h</code> while you were developing your script.  You did a few runs… <code>h</code> got redefined a few times.  Then you changed it and got rid of <code>h</code> as you are debugging.  The reason it might not flag a subsequent run with an error, is that <code>h</code> is still defined, from a previous run!  You could get your script finalized (so you think), and it tests perfectly.  Then you shut Notepad++ down, sleep, and restart N++ the next day, and use your script, to get an error like this one:</p>
<p dir="auto"><img src="/assets/uploads/files/1676132068705-1aaed32a-287b-40b3-9564-5e52ea7aa994-image.png" alt="1aaed32a-287b-40b3-9564-5e52ea7aa994-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">This is a “problem” with script commands run with global scope.  Variables from earlier runs can be used in new runs.  Either don’t execute script commands so that they set variables in the global namespace (use <code>def main():</code> and <code>main()</code>…) or, before deciding you are finished with a script, exit N++, restart N++, and test your script again.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84073</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84073</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Sat, 11 Feb 2023 16:16:46 GMT</pubDate></item><item><title><![CDATA[Reply to How to Stop Editor Replace (Python Script) from Inserting New Lines? on Sat, 11 Feb 2023 15:46:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a><br />
So that’s how it is. At first I tried to add <code>.strip("\n")</code> after <code>l.split("*")</code>, but that caused the script to stop at the first replacement or stopped working altogether (I forget which one because I tried some things). I did it wrongly after all.<br />
Now that I think about it why would I write “h” as the object lol. Then again, unlike in Python IDLE, I was not given any error warning for such a mistake. Anyway, thanks a lot!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84072</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84072</guid><dc:creator><![CDATA[Mayonnaisu]]></dc:creator><pubDate>Sat, 11 Feb 2023 15:46:20 GMT</pubDate></item><item><title><![CDATA[Reply to How to Stop Editor Replace (Python Script) from Inserting New Lines? on Sat, 11 Feb 2023 12:53:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mayonnaisu" aria-label="Profile: Mayonnaisu">@<bdi>Mayonnaisu</bdi></a></p>
<p dir="auto">A line by definition (at least my definition) always has a line-ending character(s) included.  Thus, when you get a line (<code>l</code>) from your file with <code>for l in h:</code> (which, where did <code>h</code> come from as your file object is <code>f</code>??), your <code>l</code> variable will, for the first line, contain:</p>
<p dir="auto"><code>let's*why\r\n</code></p>
<p dir="auto">So from there it is easy to see why you get extra lines in your output, isn’t it?  Your <code>s[0]</code> will be <code>let's</code> and <code>s[1]</code> will be <code>why\r\n</code>.  This assumes the typical Windows line-ending of <code>\r\n</code> (carriage return + line feed).</p>
<p dir="auto">The way I do such a section of code is:</p>
<pre><code class="language-py">with open('test.txt') as f:
    for line in f:
        line = line.rstrip()         # &lt;-- the key change from how OP is doing it!
        # ...
</code></pre>
<p dir="auto">The <code>.rstrip()</code> removes whitespace from the end of the <code>line</code> string.  If you want to be really “safe” and only remove line-ending characters, change it to <code>.rstrip('\n\r')</code>.   (Whitespace is defined as more than line-ending characters).</p>
<p dir="auto">BTW, this is really a Python question, not a PythonScript question.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/84070</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/84070</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Sat, 11 Feb 2023 12:53:26 GMT</pubDate></item></channel></rss>