# Removing 'New Line' from a python script

**URL:** <https://discourse.nodered.org/t/removing-new-line-from-a-python-script/25261>\
**Category:** General\
**Created:** [21 April 2020 19:27 UTC](https://discourse.nodered.org/t/removing-new-line-from-a-python-script/25261 "2020-04-21T19:27:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajaymccollum](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@ajaymccollum](https://discourse.nodered.org/u/ajaymccollum)\
**Post date:** [21 April 2020 19:27 UTC](https://discourse.nodered.org/t/removing-new-line-from-a-python-script/25261/1 "2020-04-21T19:27:01Z")

</div>

I've got a python script running the input for a 16 key matrix keypad. I've got a node that runs the script and outputs the keypad's inputs. After every keystroke, it adds a line break or something that I don't know enough about programming to remove from the code itself.  
I was hoping there was a way to filter that line break (or whatever it's called) out in node red and just leave the character.

 ![node-red-snip](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6dcd0f31dfbd08227e5f619a959f72331833987b.jpeg)  
I've figured out an extremely round-about way that works, but is laughably inefficient, involving a split and a switch with 16 outputs; just not pleasant to work with.  
Thanks for the time.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [21 April 2020 20:08 UTC](https://discourse.nodered.org/t/removing-new-line-from-a-python-script/25261/2 "2020-04-21T20:08:16Z")

</div>

use the string.replace() method [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/String/replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) in a function node with this regex:  
[https://regex101.com/r/dUyoA9/1](https://regex101.com/r/dUyoA9/1)  
something like:

```auto
msg.payload = msg.payload.replace(/[\n\r]+/g,““);
return msg;

```

not tested as im on my phone  
Johannes

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 April 2020 20:17 UTC](https://discourse.nodered.org/t/removing-new-line-from-a-python-script/25261/3 "2020-04-21T20:17:41Z")

</div>

I _think_ trim() will do it too.  
`msg.payload = msg.payload.trim();`  
`return msg`

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [21 April 2020 20:23 UTC](https://discourse.nodered.org/t/removing-new-line-from-a-python-script/25261/4 "2020-04-21T20:23:39Z")

</div>

never tried trim with a carriage return, would be good to know if it works.

---

<div class="post-metadata">

**Author:** ![ajaymccollum](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@ajaymccollum](https://discourse.nodered.org/u/ajaymccollum)\
**Post date:** [21 April 2020 20:32 UTC](https://discourse.nodered.org/t/removing-new-line-from-a-python-script/25261/5 "2020-04-21T20:32:58Z")

</div>

Trim did the trick! Thanks for the help, guys. Man, you all are fast on the response here. You all need to get out, hang out with friends in publ......oh yeah.....

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 May 2020 20:33 UTC](https://discourse.nodered.org/t/removing-new-line-from-a-python-script/25261/6 "2020-05-05T20:33:02Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
