Handing conversion from bytes to string when not explicitly opening a file in Python 3
<p>I am using the Requests module to authorise and then pull csv content from a web API and have it running fine in Python 2.7. I now want to write the same script in Python 3.5 but experiencing some issues:</p>

<pre><code>"iterator should return strings, not bytes (did you open the file in text mode?)"
</code></pre>

<p>The <code>requests.get</code> seems to return bytes and not a string, which seems to be related to the encoding issues seen when moving to Python 3.x. The error is raised on the 3rd from last line: <code>next(reader)</code>. In Python 2.7 this was not an issue because the csv functions were handled in <code>'wb'</code> mode.</p>

<p>This article is very similar, but as I'm not opening a csv file directly, I cant seem to force the response text to be encoded this way:
<a href="https://stackoverflow.com/questions/8515053/csv-error-iterator-should-return-strings-not-bytes">csv.Error: iterator should return strings, not bytes</a></p>

<pre><code>countries = ['UK','US','CA']
datelist = [1,2,3,4]
baseurl = 'https://somewebsite.com/exporttoCSV.php'

#--- For all date/cc combinations
for cc in countries:
    for d in datelist:

        #---Build API String with variables
        url = (baseurl + '?data=chart&amp;output=csv' +
               '&amp;dataset=' + d + 
               '&amp;cc=' + cc)

        #---Run API Call and create reader object
        r = requests.get(url, auth=(username, password))
        text = r.iter_lines()
        reader = csv.reader(text,delimiter=',')

        #---Write csv output to csv file with territory and date columns
        with open(cc + '_'+ d +'.csv','wt', newline='') as file:
            a = csv.writer(file)
            a.writerow(['position','id','title','kind','peers','territory','date']) #---Write header line
            next(reader) #---Skip original headers
            for i in reader:
                a.writerow(i +[countrydict[cc]] + [datevalue])
</code></pre>