Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

csv with different encoding [duplicate]

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)

like image 298
Tomasz Brzezina Avatar asked May 15 '26 01:05

Tomasz Brzezina


2 Answers

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='"')
like image 162
Tomasz Brzezina Avatar answered May 16 '26 18:05

Tomasz Brzezina


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.

like image 33
zigg Avatar answered May 16 '26 20:05

zigg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!