Python CSV parsing not returning proper rows

前端 未结 3 1469
独厮守ぢ
独厮守ぢ 2021-01-28 01:58

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.         


        
3条回答
  •  [愿得一人]
    2021-01-28 02:35

    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.

提交回复
热议问题