我有这个代码:
from BeautifulSoup import BeautifulSoup
TABLE_CONTENT = [['958','<a id="958F" href="javascript:c_row(\'958\')" title="go to map"><img src="/images/c_map.png" border="0"></a>','USA','Atmospheric','<a href="javascript:c_ol(\'958\')" title="click date time to show origin_list (evid=958)">1945/07/16 11:29:45</a>','33.6753','-106.4747','','-.03','21','','','TRINITY',' ',' ','<a href="javascript:c_md(\'958\')" title="click here to show source data">SourceData</a>',' '],['959','<a id="959F" href="javascript:c_row(\'959\')" title="go to map"><img src="/images/c_map.png" border="0"></a>','USA','Atmospheric','<a href="javascript:c_ol(\'959\')" title="click date time to show origin_list (evid=959)">1945/08/05 23:16:02</a>','34.395','132.4538','','-.58','15','','','LITTLEBOY',' ',' ','<a href="javascript:c_md(\'959\')" title="click here to show source data">SourceData</a>',' ']]
EVENT_LIST = []
for EVENT in TABLE_CONTENT:
events = {}
for index, item in enumerate(EVENT):
if index == 0:
events['id'] = item
if index == 4:
soup = BeautifulSoup(item)
for a in soup.findAll('a'):
events['date'] = ''.join(a.findAll(text=True))
if index == 2:
events['country'] = item
if index == 3:
events['type'] = item
if index == 5:
events['lat'] = item
if index == 6:
events['lon'] = item
if index == 8:
events['depth'] = item
if index == 9:
events['yield'] = item
if index == 12:
events['name'] = item
sorted(events, key=lambda key: events['id'])
EVENT_LIST.append(events)
print '=== new record ==='
EVENT_LIST.sort(key=lambda x: x['id'])
print EVENT_LIST
我遇到的第一个问题是,在 EVENT_LIST 中,字典对象的顺序与它们添加到列表中的顺序不同,例如,当我打印结果时,'lat' 和 'lon' 不是按顺序排列的:
[{'name': 'TRINITY', 'country': 'USA', 'lon': '-106.4747', 'yield': '21', 'lat': '33.6753', 'depth': '-.03', 'date': u'1945/07/16 11:29:45', 'type': 'Atmospheric', 'id': '958'}, {'name': 'LITTLEBOY', 'country': 'USA', 'lon': '132.4538', 'yield': '15', 'lat': '34.395', 'depth': '-.58', 'date': u'1945/08/05 23:16:02', 'type': 'Atmospheric', 'id': '959'}]
还有更好的方法来编写这段代码吗?