1

I've been searching and searching, but can't find a way to have a \n in a string converted (parsed?) while just manipulating it.

Here's what I'm trying to do: I receive a message (via MQTT if that matters) that's just a string. For example it might be "Doorbell\nPressed".

(I have control over this string, so it can be anything, but only one line. I can't insert an actual newline character in the string though, it's a limitation of the interface to input the string)

I am passing this string to another program, and want the new program to receive:

Doorbell
Pressed

Not the:

"Doorbell\nPressed" 

I can't seem to figure out how to do this? If it helps, here's the code:

def on_message(client, userdata, msg):  # The callback for when a PUBLISH message is received from the server.
    print("Message received-> " + msg.topic + " " + str(msg.payload))  # Print a received msg
       subprocess.Popen(["/usr/local/bin/signal-cli", "-u","+12125551212", "send", "-m", str(msg.payload), "+12125551213"])

where msg.payload is the actual string "Doorbell\nPressed".

Interestingly the print of the message prints "Doorbell\nPressed" instead of the expected:

Doorbell
Pressed

The print output looks like this:

Message received-> queue/signal_message Doorbell\nPressed

What am I missing?

rawrex
  • 4,044
  • 2
  • 8
  • 24
  • So you want a pepperoni pizza with pepperoni instead of pepperoni? – Julien Jun 28 '21 at 07:56
  • I see that you are using `signal-cli`. Please check if [this](https://github.com/AsamK/signal-cli/issues/105) helps you. – AKS Jun 28 '21 at 08:15
  • If I run the `print()` call with the data as you describe it, I get the line `Message received-> TOPIC Doorbell` followed by the line `Pressed` which is exactly what I would expect. – BoarGules Jun 28 '21 at 08:22
  • You seems really confused by something so let me clarify: the `\n` is a representation of a *value* stored in some memory. This value can be interpreted or represented in various way. It correspond to the value 10 in decimal for instance. When you want to display (or print) this value using some program then you have to tell the program what representation you want, or how it should interpret this value. However, in most of case programs have a default behavior. In your case the default behavior of the program that receive the data is to print it as '\n' instead of an actual line break. – Welgriv Jun 29 '21 at 09:14

0 Answers0