I\'m new to python and having issues with the CSV parser. Here is the code:
import urllib2
import csv
u = urllib2.urlopen(r\'http://finance.yahoo.com/d/quotes.
csv.reader
factory takes as its first argument a list or other iterable iterable of lines, not the full string with line breaks in it (which it will just iterate over by character). Use data = u.readlines()
instead of data = u.read()
.
Quote:
The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. There is no “CSV standard”, so the format is operationally defined by the many applications which read and write it. The lack of a standard means that subtle differences often exist in the data produced and consumed by different applications. These differences can make it annoying to process CSV files from multiple sources. Still, while the delimiters and quoting characters vary, the overall format is similar enough that it is possible to write a single module which can efficiently manipulate such data, hiding the details of reading and writing the data from the programmer.
try to make your own csv and see if it works, if yes then there is something different about their csv format.
Just parse u
directly into the reader:
import urllib2
import csv
u = urllib2.urlopen(r'http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=nab')
reader = csv.reader(u)
for row in reader:
print row
The problem is that csv.reader
accepts an iterable of lines. When you pass it a string, it thinks each character is a line. In fact, the reason it doesn't just give single-character elements is due the the quotation marks.
u
is already an iterable of lines, so it's fine to just pass in.