Importing CSV from URL and displaying rows on Python by using Requests
Question: 
    import csv
    import requests
    webpage = requests.get('http://www.pjm.com/pub/account/lmpda/20160427-da.csv')
    reader=csv.reader(webpage)
    for row in reader:
        print(row)
    

Hi, I'm new to Python and I'm trying to open a CSV file from a URL & then
display the rows so I can take the data that I need from it. However, the I
get an error saying :

> Traceback (most recent call last): File "", line 1, in for row in reader:
> Error: iterator should return strings, not bytes (did you open the file in
> text mode?)

Thank you in advance.


Answer: Use _.text_ as you are getting _bytes_ returned in python3:

    
    
    webpage = requests.get('http://www.pjm.com/pub/account/lmpda/20160427-da.csv')
    reader = csv.reader([webpage.text])
    for row in reader:
        print(row)
    

That gives `_csv.Error: new-line character seen in unquoted field` so split
the lines after decoding, also `stream=True` will allow you to get the data in
chunks not all at once so you can filter by row and write:

    
    
    import csv
    import requests
    
    
    webpage = requests.get('http://www.pjm.com/pub/account/lmpda/20160427-da.csv', stream=1)
    
    for line in webpage:
         print(list(csv.reader((line.decode("utf-8")).splitlines()))[0])
    

Which gives you:

    
    
    ['Day Ahead Hourly LMP Values for 20160427', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['00', '600', '700', '800', '900', '1000', '1100', '1200', '1300', '1400', '1500', '1600', '1700', '1800', '1900', '2000', '2100', '2200', '2300', '2400', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['1', '25.13', '25.03', '28.66', '25.94', '21.74', '19.47', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    ['600', '600', '600', '700', '700', '700', '800', '800', '800', '900', '900', '900', '1000', '1000', '1000', '1100', '1100', '1100', '1200', '1200', '1200', '1300', '1300', '1300', '1400', '1400', '1400', '1500', '']
    ['1500', '1500', '1600', '1600', '1600', '1700', '1700', '1700', '1800', '1800', '1800', '1900', '1900', '1900']
    ['', '2000', '2000', '2000', '2100', '2100', '2100', '2200', '2200', '2200', '2300', '2300', '2300', '2400', '2400', '2400', '']
    ['lLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'Tot']
    ['alLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'To']
    ['talLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'TotalLMP', 'CongestionPrice', 'MarginalLossPrice', 'T']
    .......................................
    

