Possible Duplicate:
Open a file in the proper encoding automatically
my code:
import csv
def handle_uploaded_file(f):
dataReader = csv.reader(f, delimiter=';', quotechar='"')
for row in dataReader:
do_sth
the problem is that it works well only if csv is UTF-8 encoded. What should I change to serve the iso-8859-2 or windows-1250 encoding? (the best solution is to autorecognize the encoding, but hand converting is also acceptable)
The solution for now:
def reencode(file):
for line in file:
yield line.decode('windows-1250').encode('utf-8')
csv_reader = csv.reader(reencode(open(filepath)), delimiter=";",quotechar='"')
Have a look at the examples section of the csv module documentation. At the end, you'll find classes you can use for exactly that purpose, specifying the encoding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With