CodexBloom - Programming Q&A Platform

Python 2.7: Handling UnicodeDecodeError while reading CSV files with mixed encodings

👀 Views: 469 đŸ’Ŧ Answers: 1 📅 Created: 2025-08-07
python-2.7 csv encoding unicode Python

Hey everyone, I'm running into an issue that's driving me crazy... I'm working on a personal project and I'm trying to read a CSV file in Python 2.7 that contains a mix of UTF-8 and ISO-8859-1 encoded data. Whenever I attempt to read the file using the `csv` module, I get the following behavior: ``` UnicodeDecodeError: 'utf8' codec need to decode byte 0xe9 in position 10: invalid continuation byte ``` I've tried opening the file in binary mode and then decoding each row, but I still run into issues with mixed encodings. My current approach looks like this: ```python import csv with open('data.csv', 'rb') as f: reader = csv.reader(f) for row in reader: print(row) ``` I also attempted to use the `codecs` module to handle the encoding, like this: ```python import codecs import csv with codecs.open('data.csv', 'r', 'utf-8') as f: reader = csv.reader(f) for row in reader: print(row) ``` However, this approach gives me a different behavior when working with ISO-8859-1 encoded rows. Ideally, I would like to read the file and process it without losing any data, regardless of the encoding used in different rows. Is there a reliable way to handle this situation in Python 2.7? Any help would be greatly appreciated! For context: I'm using Python on macOS. Has anyone else encountered this?