Python, UnicodeEncodeError
Question: Hello I've got this piece of code

    
    
    import urllib.request
    import string
    import time
    import gzip
    from io import BytesIO
    from io import StringIO
    from zipfile import ZipFile
    import csv
    import datetime
    from datetime import date
    import concurrent.futures
    
    den = date.today().replace(day=1) - datetime.timedelta(days=1)
    url = '' + den.strftime("%Y%m%d") + '_OB_ADR_csv.zip'
    
    data = urllib.request.urlopen(url).read()
    zipdata = BytesIO()
    zipdata.write(data)
    
    csvfile = open('./test.csv', 'w', newline='')
    csvwrite = csv.writer(csvfile, delimiter=';')
    
    with ZipFile(zipdata) as zip:
        for i, nazev in enumerate(zip.namelist()):
            if i == 0:
                continue
    
            csvstring = StringIO(str(zip.read(nazev), encoding='windows-1250'))
            csvreader = csv.reader(csvstring, delimiter=';')
    
            for j, row in enumerate(csvreader):
                if j == 0 and i != 1:
                    continue
    
                csvwrite.writerow(row)
    
    csvfile.close()
    

When i run it it sometimes throws "UnicodeEncodeError: 'ascii' codec can't
encode character '\xf3' in position 1: ordinal not in range(128)" at
"csvwrite.writerow(row)"

How can I solve this issue? Thank you.

EDIT: I run it under Python 3.3


Answer: You didn't tell csv.writer about the encoding. Take a look at the [pydocs for
the csv module](http://docs.python.org/3.2/library/csv.html):

> To decode a file using a different encoding, use the encoding argument of
> open...[t]he same applies to writing in something other than the system
> default encoding: specify the encoding argument when opening the output
> file.

You can see from the UnicodeEncodeError that Python thinks you want the file
written in ascii. Just specify the encoding parameter and choose your desired
encoding (my suggestion is `encoding='utf-8'`).

