使用namedtuple文档例子作为我在Python 3.3的模板,我有以下的代码来下载一个CSV,把它变成了一系列namedtuple子类的实例:有没有更好的方法来使用urlopen做csv/namedtuple?
from collections import namedtuple
from csv import reader
from urllib.request import urlopen
SecurityType = namedtuple('SecurityType', 'sector, name')
url = 'http://bsym.bloomberg.com/sym/pages/security_type.csv'
for sec in map(SecurityType._make, reader(urlopen(url))):
print(sec)
这引发了以下异常:
Traceback (most recent call last):
File "scrap.py", line 9, in <module>
for sec in map(SecurityType._make, reader(urlopen(url))):
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
我知道问题是urlopen返回的是字节而不是字符串,而且我需要在某个时刻解码输出。以下是我正在做的,现在,使用StringIO的:
from collections import namedtuple
from csv import reader
from urllib.request import urlopen
import io
SecurityType = namedtuple('SecurityType', 'sector, name')
url = 'http://bsym.bloomberg.com/sym/pages/security_type.csv'
reader_input = io.StringIO(urlopen(url).read().decode('utf-8'))
for sec in map(SecurityType._make, reader(reader_input)):
print(sec)
这闻起来很有趣,因为我基本上遍历字节的缓冲区,解码,停顿,然后遍历新的字符串缓冲区。没有两次迭代,是否有更多的Pythonic方法来做到这一点?
嗯。我要推荐的东西(使用'TextIOWrapper')不起作用,它应该有IMO。一些挖掘显示[这个错误(问题16723)](http://bugs.python.org/issue16723),这似乎是问题。 – DSM 2013-05-04 13:54:14
@DSM:该补丁已在2月份应用,因此最新的3.x版本包含它。您使用的是什么版本的3.x? – 2013-05-04 13:59:06
3.3.0。我喜欢保持最新状态,但自从3.3.1发布以来还没有一个月。 :^) – DSM 2013-05-04 14:01:37