我使用
map(lambda x: x[0], cur.description)
构建了一个映射表,然后在 for 循环中使用它:for r in rows:
proxy_list.append(Proxy(dict(zip(columns, [e for e in r]))))
但是我发现结果很奇怪。只有第一个zip成功了,其他的都返回了{}
。
测试样例:
r = ('204.93.54.15', '7808', 6, 0, '', '2013-11-12 20:27:54', 0, 3217.0, 'United States', 'HTTPS')
description = (('ip', None, None, None, None, None, None), ('port', None, None, None, None, None, None), ('level', None, None, None, None, None, None), ('active', None, None, None, None, None, None), ('time_added', None, None, None, None, None, None), ('time_checked', None, None, None, None, None, None), ('time_used', None, None, None, None, None, None), ('speed', None, None, None, None, None, None), ('area', None, None, None, None, None, None), ('protocol', None, None, None, None, None, None))
columns = map(lambda x: x[0], description)
我的测试结果如下:
>>> dict(zip(columns, [e for e in r]))
{'protocol': 'HTTPS', 'level': 6, 'time_used': 0, 'ip': '204.93.54.15', 'area': 'United States', 'port': '7808', 'active': 0, 'time_added': '', 'speed': 3217.0, 'time_checked': '2013-11-12 20:27:54'}
>>> zip(columns, [e for e in r])
<zip object at 0x0000000004079848>
>>> dict(zip(columns, [e for e in r]))
{}
list(zip(columns, [e for e in r]))
。 - Ashwini Chaudhary[]
,Proxy
是一个只接受一个字典类型参数的类。 - Mithrilshlex
和open
时遇到过这种情况。 - bozdozcolumns
和r
的类型是什么? - Ashwini Chaudharyr = list(r)
吗?并分享一下是否得到了相同的结果? - UltraInstinct