1

我在这里有一个元组列表:

import datetime
import requests
from operator import itemgetter

original = [(datetime.datetime(2013, 11, 12, 19, 24, 50), u'78:E4:00:0C:50:DF', u' 8', u'Hon Hai Precision In', u''), (datetime.datetime(2013, 11, 12, 19, 24, 50), u'78:E4:00:0C:50:DF', u' 8', u'Hon Hai Precision In', u''), (datetime.datetime(2013, 11, 12, 19, 24, 48), u'9C:2A:70:69:81:42', u' 5', u'Hon Hai Precision In 12:', u''), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'00:1E:4C:03:C0:66', u' 9', u'Hon Hai Precision In', u''), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'20:C9:D0:C6:8F:15', u' 8', u'Apple', u''), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'68:5D:43:90:C8:0B', u' 11', u'Intel Orate', u' MADEGOODS'), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'68:96:7B:C1:76:90', u' 15', u'Apple', u''), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'68:96:7B:C1:76:90', u' 15', u'Apple', u''), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'04:F7:E4:A0:E1:F8', u' 32', u'Apple', u''), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'04:F7:E4:A0:E1:F8', u' 32', u'Apple', u'')]

data = [x[:-2] for x in original]

newData = sorted(data, key=itemgetter(0))

print newData

[(datetime.datetime(2013, 11, 12, 19, 24, 47), u'00:1E:4C:03:C0:66', u' 9'), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'20:C9:D0:C6:8F:15', u' 8'), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'68:5D:43:90:C8:0B', u' 11'), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'68:96:7B:C1:76:90', u' 15'), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'68:96:7B:C1:76:90', u' 15'), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'04:F7:E4:A0:E1:F8', u' 32'), (datetime.datetime(2013, 11, 12, 19, 24, 47), u'04:F7:E4:A0:E1:F8', u' 32'), (datetime.datetime(2013, 11, 12, 19, 24, 48), u'9C:2A:70:69:81:42', u' 5'), (datetime.datetime(2013, 11, 12, 19, 24, 50), u'78:E4:00:0C:50:DF', u' 8'), (datetime.datetime(2013, 11, 12, 19, 24, 50), u'78:E4:00:0C:50:DF', u' 8')]

每个元组中的第一个元素是日期/时间,第二个是 MAC 地址,第三个是 RSSI 强度。

我正在寻找将每个元组发送到 Google 测量协议的 POST 请求中的最佳方式,如下所示:

requests.post("http://www.google-analytics.com/collect", 
              data="v=1&tid=UA-22560594-2&cid="varMACADDRESS"&t=event&ec="varDATETIME"&ea="varRSSI")

“varXXXXXX”代表元组的元素。

这就是我认为应该是解决方案,但我想不出如何将每个元组的元素分配给 %s 的:

for tuples [:10] in newData:
    requests.post("http://www.google-analytics.com/collect", 
              data="v=1&tid=UA-22560594-2&cid="%s"&t=event&ec="%s"&ea="%s")

什么是最有效和最pythonic的方式来做到这一点?

4

2 回答 2

2

只需利用您可以为 kwarg 指定 a 的事实,dict并且datarequests 将为您处理表单编码

for date,mac,rssi in some_collection_of_tuples:
    payload = {'t':'event','v':'1','ec':date,'cid':mac,...} #etc
    requests.post("http://www.google-analytics.com/collect", data=payload)
于 2013-11-13T04:11:03.740 回答
1

您可以在遍历列表时解压缩元组值并使用格式插入值。

for date, mac, rssi in newData:
    requests.post("http://www.google-analytics.com/collect", 
          data="v=1&tid=UA-22560594-2&cid={}&t=event&ec={}&ea={}".format(
              mac, 
              date, 
              rssi)
    )
于 2013-11-13T04:10:11.457 回答