I’m attempting to write a small python script that processes through CSV data. I read the URL and the data is there when I print the variable, but when I parse it using either csv.reader or csv.DictReader it only reads the first character as a field name and then the next character as the value for it. I don’t understand what I’m doing wrong and I would appreciate the help.
Here’s my code:
import sys
import os
import urllib2
import csv
response = urllib2.urlopen('http://ichart.finance.yahoo.com/table.csv?s=YHOO&d=0&e=28&f=2010&g=d&a=3&b=12&c=2009&ignore=.csv')
html = response.read()
informed = csv.DictReader(html)
for row in informed:
print row
which prints out a dictionary of [‘D’:’a’] for the first row, and all the rest are character pairs. Any help is appreciated. I’m using python 2.6 on Windows.
According to the csv module docs, you need to pass a file or file-like object to the reader, not a string. I’m not familiar with what
urlopenreturns, but you could try passingresponseto the reader. Or, you could use a StringIO object which is for just this purpose –html = StringIO.StringIO(response.read())ought to do it.