Iterators from urllib2

Joshua Ginsberg listspam at flowtheory.net
Fri Jul 22 10:53:05 EDT 2005


I'm a bit baffled by something...

In a script I wrote, I have defined a function that runs  
urllib2.urlopen() on a urllib2.Request object and returns the file-like  
object. The code that calls this function attempts to build a  
csv.DictReader object based on that file-like object, but an error is  
thrown saying the file-like object is not an iterator.

I could have sworn urllib2.urlopen returned an iterator, so I tested:

Python 2.3 (#1, Sep 13 2003, 00:49:11)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import urllib2
 >>> r = urllib2.Request('http://www.google.com')
 >>> ifs = urllib2.urlopen(r)
 >>> dir(ifs)
['__doc__', '__init__', '__iter__', '__module__', '__repr__', 'close',  
'fileno', 'fp', 'geturl', 'headers', 'info', 'next', 'read',  
'readline', 'readlines', 'url']

Yep. But what about in my code? I modify my code to print dir(ifs)  
before creating the DictReader...

['__doc__', '__init__', '__module__', '__repr__', 'close', 'fp',  
'geturl', 'headers', 'info', 'read', 'readline', 'url']
Traceback (most recent call last):
   File "CSVParser.py", line 144, in ?
     print parseQHost(circuits[cktname], cktname)
   File "CSVParser.py", line 126, in parseQHost
     r = csv.DictReader(ifs, fieldlist)
   File  
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
python2.3/csv.py", line 100, in __init__
     self.reader = reader(f, dialect, *args)
TypeError: argument 1 must be an iterator

Whoa! Where did the __iter__, readlines, and next attributes go? Ideas?

-jag





More information about the Python-list mailing list