In [40]:
from lxml import etree
In [41]:
with open('USA_Counties_with_FIPS_and_names.svg') as f:
    doc = etree.fromstring(f.read().encode('utf-8'))
In [42]:
for i,item in enumerate(doc):
    print('-------------------------------')
    if 'style' in item.attrib:
        print(item.attrib['style'])
    if i > 5: break
-------------------------------
-------------------------------
-------------------------------
-------------------------------
font-size:12px;fill:#d0d0d0;fill-rule:nonzero;stroke:#000000;stroke-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel
-------------------------------
font-size:12px;fill:#d0d0d0;fill-rule:nonzero;stroke:#000000;stroke-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel
-------------------------------
font-size:12px;fill:#d0d0d0;fill-rule:nonzero;stroke:#000000;stroke-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel
-------------------------------
font-size:12px;fill:#d0d0d0;fill-rule:nonzero;stroke:#000000;stroke-opacity:1;stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;marker-start:none;stroke-linejoin:bevel
In [43]:
from numpy import *
def foo(x): return hex( min(255,int(x*256)) )[2:].zfill(2)
def goo(r,g,b): return '#'+foo(r)+foo(g)+foo(b)
def randomcolor(pallor=0): 
    c = random.rand(3)
    d = (1-pallor)*c + pallor*(ones(3))
    return goo(*d)
In [44]:
randomcolor()
Out[44]:
'#bab997'
In [45]:
for i,item in enumerate(doc):
    #print('-------------------------------')
    if 'style' in item.attrib:
        item.attrib['style'] = item.attrib['style'].replace('fill:#d0d0d0','fill:'+randomcolor(.8))
with open('coloredmap.svg','w') as f:
    f.write(etree.tostring(doc).decode('utf-8'))