Please critique my script

MRAB python at mrabarnett.plus.com
Thu Jul 14 15:12:05 EDT 2011


[snip]
raw_input() returns a string, so there's no need for these 3 lines:

 > y = str(y)
 > z = str(z)
 > p = str(p)

 > pagedef = ("http://www.localcallingguide.com/xmllocalprefix.php?npa=" 
+ y + "&nxx=" + z)
 > print "Querying", pagedef
 >
 > #------Get info from NANPA.com ----------
 > urllib2.install_opener(opener)
 > page = urllib2.urlopen(pagedef)
 > soup = BeautifulSoup(page)
 > soup = str(soup)
 >
 > #------Parse Gathered Data----------
 > for line in npaReg.findall(soup):
 >     npalist.insert(count, line)
 >     count = count + 1
 >
 > for line2 in nxxReg.findall(soup):
 >     nxxlist.insert(count2, line2)
 >     count2 = count2 + 1
 >
enumerate will help you here:

for count, line in enumerate(npaReg.findall(soup)):
     npalist.insert(count, line)

for count2, line2 in enumerate(nxxReg.findall(soup)):
     nxxlist.insert(count2, line2)

 > #-----Sort, remove duplicates, concatenate the last digits for 
similiar NPA/NXX ------
 > for makenewlist in range(0, count):
 >     sortlist.append(npalist.pop(0) + nxxlist.pop(0))
 >
 > sortlist.sort()
 >
 > for sortednumber in sortlist:
 >     if sortednumber not in sortedlist:
 >         sortedlist.append(sortednumber)
 >
If you're going to sort them anyway (so you don't need to preserve the 
existing order), the easiest way to remove duplicates is to use a set:

sortedlist = sorted(set(sortlist))

[snip]



More information about the Python-list mailing list