Python字符串拆分为查询字符串

时间:2014-08-15 14:51:20

标签: python linux

我写了这段代码来通过查询字符串发送数据,但我无法弄清楚如何获得'%0A'超出了对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)

查询字符串如下所示:

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

进入文件的数据如下所示:

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

如何摆脱'%0A'在查询字符串中的RSSI字段之后? 我在Raspberry Pi上使用Raspbian。

2 个答案:

答案 0 :(得分:2)

%0A是换行符(http://www.asciitable.com/)。发生这种情况是因为在读取输入行时没有strip换行符。只需在顶部附近添加line = line.strip()即可。

答案 1 :(得分:1)

好吧,0x0a是十进制10 - 而ascii表说这是新行。因此,您可以调用strip()来修剪字符串 -

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