通过一个元组列表循环,使每个元组POST请求(Looping through a list of t

2019-10-19 00:01发布

我这里有元组的列表:

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强度。

我正在寻找最好的方式来发送每个元组在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代表所述元组的元素。

这就是我认为应该是解决方案,但我想不出如何分配每个元组到%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")

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

Answer 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)
    )


Answer 2:

只要采取的事实,即你可以指定一个dictdata kwarg并请求将处理表单编码的你 。

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)


文章来源: Looping through a list of tuples to make a POST request with each tuple