Reputation: 225
I'm almost an absolute beginner in Python, but I am asked to manage some difficult task. I have read many tutorials and found some very useful tips on this website, but I think that this question was not asked until now, or at least in the way I tried it in the search engine.
I have managed to write some url in a csv file. Now I would like to write a script able to open this file, to open the urls, and write their content in a dictionary. But I have failed : my script can print these addresses, but cannot process the file.
Interestingly, my script dit not send the same error message each time. Here the last : req.timeout = timeout AttributeError: 'list' object has no attribute 'timeout'
So I think my script faces several problems : 1- is my method to open url the right one ? 2 - and what is wrong in the way I build the dictionnary ?
Here is my attempt below. Thanks in advance to those who would help me !
import csv
import urllib
dict = {}
test = csv.reader(open("read.csv","rb"))
for z in test:
sock = urllib.urlopen(z)
source = sock.read()
dict[z] = source
sock.close()
print dict
Upvotes: 0
Views: 1083
Reputation: 11585
First thing, don't shadow built-ins. Rename your dictionary to something else as dict
is used to create new dictionaries.
Secondly, the csv
reader creates a list per line that would contain all the columns. Either reference the column explicitly by urllib.urlopen(z[0]) # First column in the line
or open the file with a normal open()
and iterate through it.
Apart from that, it works for me.
Upvotes: 1