Thomas Hall
Thomas Hall

Reputation: 43

Python string splitting into query string

I wrote this code to send data by query string, but I cannot figure out how to get the '%0A' out of the query sting for the RSSI.

#!/usr/bin/python
import fileinput
import sys
import requests
import socket

for line in fileinput.input():
   if line.startswith("UUID:"):
      a = line.split(" ")[1]
      b = line.split(" ")[3]
      c = line.split(" ")[5]
      d = line.split(" ")[7]
      e = line.split(" ")[9]
      f = socket.gethostname()
      payload = {'uuid': a, 'major': b, 'minor': c, 'power': d, 'rssi': e, 'hubname': f}
      r = requests.get("http://posttestserver.com/post.php", params=payload)
      print(r.url)

The query string looks like this:

http://posttestserver.com/post.php?major=1&hubname=pihub0001&uuid=2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6&power=-71&rssi=-65%0A&minor=1

The data coming into the file looks like this:

iBeacon Scan ...
UUID: 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 MAJOR: 1 MINOR: 1 POWER: -71 RSSI: -61
UUID: 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 MAJOR: 1 MINOR: 1 POWER: -71 RSSI: -68
UUID: 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 MAJOR: 1 MINOR: 1 POWER: -71 RSSI: -68
UUID: 3F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 MAJOR: 1 MINOR: 1 POWER: -71 RSSI: -65
UUID: 2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 MAJOR: 1 MINOR: 1 POWER: -71 RSSI: -68

How do I get rid of the '%0A' after the RSSI field in the query string? I'm using Raspbian on a Raspberry Pi.

Upvotes: 0

Views: 263

Answers (2)

John Zwinck
John Zwinck

Reputation: 249273

%0A is a newline (http://www.asciitable.com/). This is happening because you do not strip the newline when reading the input line. Just add line = line.strip() near the top.

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201447

Well, 0x0a is decimal 10 - and the ascii table says that is new line. So, you could call strip() to trim the String -

e = line.split(" ")[9].strip()

Upvotes: 1

Related Questions