Use io.TextIOWrapper()
to decode the urllib
response:
reader_input = io.TextIOWrapper(urlopen(url), encoding='utf8', newline='')
Now csv.reader
is passed the exact same interface that it would get when opening a regular file on the filesystem in text mode.
With this change your example URL works for me on Python 3.3.1:
>>> for sec in map(SecurityType._make, reader(reader_input)):
... print(sec)
...
SecurityType(sector='Market Sector', name='Security Type')
SecurityType(sector='Comdty', name='Calendar Spread Option')
SecurityType(sector='Comdty', name='Financial commodity future.')
SecurityType(sector='Comdty', name='Financial commodity generic.')
SecurityType(sector='Comdty', name='Financial commodity option.')
...
SecurityType(sector='Muni', name='ZERO COUPON, OID')
SecurityType(sector='Pfd', name='PRIVATE')
SecurityType(sector='Pfd', name='PUBLIC')
SecurityType(sector='', name='')
SecurityType(sector='', name='')
SecurityType(sector='', name='')
SecurityType(sector='', name='')
SecurityType(sector='', name='')
SecurityType(sector='', name='')
SecurityType(sector='', name='')
SecurityType(sector='', name='')
SecurityType(sector='', name='')
The last lines appear to yield empty tuples; the original indeed has lines with nothing more than a comma on them.