[Tutor] better way to write this code
Dave Angel
d at davea.name
Fri Aug 24 02:00:44 CEST 2012
On 08/23/2012 02:33 PM, Norman Khine wrote:
> Hello,
> I have this code (http://pastie.org/4575790) which pulls data from a list
> and then modifies some of the values such as the 'yield' entry, which has
> entries like:
>
> 21
> 15
> ≤ 1000
> ≤ 20
> 2.2 - 30
>
> so that they are cleaned up.
>
> # -*- coding: UTF-8 -*-
> # Norman Khine <norman at zmgc.net>
> import operator, json
> from BeautifulSoup import BeautifulSoup
>
> combos={0: 'id',
> 2: 'country',
> 3: 'type',
> 5: 'lat',
> 6: 'lon',
> 12: 'name' }
>
> TABLE_CONTENT = [['958','<a id="958F" href="javascript:c_row(\'958\')"
> <snip>
> event_list = []
> for event in TABLE_CONTENT:
> event_dict = {}
> for index, item in enumerate(event):
> if index == 8:
> if item == ' ':
> event_dict['depth'] = '0'
> else:
> event_dict['depth'] = item
> if index == 9:
> try:
> <snip>
> can the code be improved further?
No idea. You mistakenly posted in html, so most of us cannot read the
indentation. This is a text forum. (Yah, I know I could read the
pastie link, but I try to stay within the list)
>
> also, the content has 2,153 items, what will be the correct way to have
> this in a separate file and import this within this file to work on it?
I assume you mean TABLE_CONTENT ?
Just move the whole assignment to a new module, perhaps content.py,
then import it as
from content import TABLE_CONTENT
--
DaveA
More information about the Tutor
mailing list