Skip to content Skip to sidebar Skip to footer

Looping Through A List Of Tuples To Make A Post Request With Each Tuple

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

Solution 1:

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.

fordate,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)

Solution 2:

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

fordate, 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)
    )

Post a Comment for "Looping Through A List Of Tuples To Make A Post Request With Each Tuple"