Looping through a list of tuples to make a POST re

2019-08-04 06:54发布

I have a list of tuples here:

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')]

The first element in each tuple is a date/time, the second is a MAC address and the third is a RSSI strength.

I am looking for the best way to send each tuple in a POST request the Google's Measurement Protocol, like so:

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

The "varXXXXXX"s represent the elements of the tuples.

This is what I think should be the solution, but I can't think of how to assign the elements of each tuple to the %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")

What would be the most efficient and pythonic way to do this?

2条回答
Luminary・发光体
2楼-- · 2019-08-04 07:33

Just take advantage of the fact that you can specify a dict to the data kwarg and requests will handle the form-encoding for you.

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)
查看更多
爷的心禁止访问
3楼-- · 2019-08-04 07:39

You can unpack the tuple values as you iterate over the list and use format to insert the values.

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)
    )
查看更多
登录 后发表回答