python - utilizing "isalpha" or "startswith" and/or troubleshooting "list index out of range error" -


i'm newbie in facets (so, python, beautifulsoup, etc), bear me please.

i trying create variety of maps different types of data following tutorial found @ flowingdata.com (how make county thematic map using free tools).

i can duplicate tutorial without error no version issues can speak of (i'm using python 2.7.5 , beautifulsoup 4.3.1 on mac os 10.8). use (more detailed) state-county maps , colorize them different data. have maps (svg) , data (csv) in appropriate files. here script running:

import csv beautifulsoup import beautifulsoup  totpop = {} reader = csv.reader(open('datafile.csv', 'ru'), delimiter=",") row in reader:     try:         id = row[0]         pop = float( row[1].strip() )         totpop[id] = pop     except:         pass  svg = open('mapfile.svg', 'r').read()  soup = beautifulsoup(svg, selfclosingtags=['defs', 'sodipodi:namedview', 'path'])  paths = soup.findall('path')  colors = ["#f1eef6", "#d4b9da", "#c994c7", "#df65b0", "#dd1c77", "#980043"]  path_style = 'fill-rule:nonzero; stroke: #ffffff; stroke-width: 5; stroke-opacity: 1; fill: '  # colorize based on data p in paths:          try:             pop = totpop[p['id']]         except:             continue          if pop > 750000:             color_class = 6         elif pop > 500000:             color_class = 5         elif pop > 250000:             color_class = 4         elif pop > 125000:             color_class = 3         elif pop > 75000:             color_class = 2         elif pop > 25000:             color_class = 1         else:             color_class = 0          color = colors[color_class]         p['style'] = path_style + color   print soup.prettify() 

and i'm getting following error:

file "scriptname.py", line 54, in color = colors[color_class] indexerror: list index out of range

("line 54" may not match because removed comment lines in sample code)

regarding svg file, has both paths , groups of paths (the groups of paths counties comprised of multiple paths). single path counties have county name "id." multi-path counties have county name group "id" nested paths have numeric ids. want style applied either path or group matches county name in data file (i'm aware sample code not deal groups right now). test, ran script on sample svg had paths (no groups) , worked brilliantly...so know right. think issue groups and/or paths (within groups) numeric ids.

how around error? tried remove groups , change multi-path ids same thing...that didn't work either. numeric ids cause problems if they're not explicitly ignored?

i'm wondering if can run script either singles out paths and/or groups have names (no numbers/digits) using sort of "isalpha" tool or "startswith" (any letter) avoid index error.

i hope provides enough information.

here link 1 of svg maps (i have stripped clippath , state_outline working file) , here link corresponding datafile

if test files, may have viewbox issues have sorted out separately.

thanks help!

from looks of it, you're assuming in following array:

colors = ["#f1eef6", "#d4b9da", "#c994c7", "#df65b0", "#dd1c77", "#980043"]

that elements here indexed 1, 2, 3, 4, 5, 6. index going begin 0 , not 1. "#f1eef6" element 0 , last element ("#980043") number 5 in array. in if pop statements, you'll need make adjustment.

also, you'll need change else statement set color_class can use determine whether should attempt grab valid color or not. thinking along lines of:

else:     color_class = null  if color_class != null     color = colors[color_class]     p['style'] = path_style + color 

i'm not familiar python syntax there may error in there idea i'm trying show here.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -