1

I use this code to create a json data package. The datapackage gets a newline character appended: 'fields': {'time': '31.495\n'} How do I get rid of this \n?

import subprocess, signal, os, pylibmc, time, datetime

# Send network ping delay time to influxdb
cmd = "ping -c 1 1.0.0.1 | tail -1| awk '{print $4}' | cut -d '/' -f 2" # pylint: disable=line-too-long
data=subprocess.check_output(cmd, shell=True).decode("utf-8")
print(data)

stamp=time.ctime()
print(id)
print("[%s] Time: %s" % (stamp, data))
print(data)
#Create the JSON data structure
data = [
      {
        "measurement": "ping_delay",
        "tags": {
        "location": location,
        },
        "time": stamp,
        "fields": {
        "delay" : data,
       }
      }
    ]
# Send the JSON data to InfluxDB
print(data)
1
  • Thanks, I got it working using your hint: { "delay" : data.rstrip() } this works. Commented Dec 9, 2021 at 21:05

2 Answers 2

1

You can make use of the strip() function in python.

    # Other code
    "time" : stamp.strip('\n')
    # Other code
Sign up to request clarification or add additional context in comments.

Comments

0

I don't completely understand your example since you don't have time element within fields. But anyway you can use rstrip to strip whitespaces from the end of a variable. https://docs.python.org/3/library/stdtypes.html#str.rstrip

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.