循环访问元组列表以使用每个元组进行POST请求 [英] Looping through a list of tuples to make a POST request with each tuple

查看:135
本文介绍了循环访问元组列表以使用每个元组进行POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 导入日期时间
导入请求$ b $来自操作员导入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在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.d (2013,11,12,19,24,47),u'68:96:7B:C1:76:90',u'15',u'Apple',u''),(datetime.datetime( u'04:F7:E4:A0:E1:F8',u'32',u'Apple',u'),(datetime.datetime(2013, 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
$ b (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: (datetime.datetime(2013,11,12,19,24,47),u'68:96:7B:C1:76:90',u'15'),(日期时间.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: (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请求中的每个元组的Google Measurement Protocol,如下所示:

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

varXXXXXX表示元组的元素。

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

 为元组[:10]在newData中:
requests.post(http:// www.google-analytics.com/collect,
data =v = 1& tid = UA-22560594-2& cid =%s& t = event& ec =%s& ea =%s)

做这件事最有效率和最好的方法是什么? / p>

解决方案

您可以在遍历列表时解开元组值,并使用 format 来插入值。

  for new,mac,rssi in newData:
requests.post(http://www.google-analytics.com/collect,
data =v = 1& tid = $ UA-22560594-2& cid = {}& t = event& ec = {}& ea = {}格式(
mac,
日期,
rssi)


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?

解决方案

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

这篇关于循环访问元组列表以使用每个元组进行POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆