I am trying to parse a CSV file (from an external data source) where one of the columns uses inconsistent character encodings. Rather than trying to get the data provider to use a consistent encoding, I would like to just read that column as binary data. However, pandas.read_csv
seems to decode the whole file to a string before parsing, so this is giving me errors (UnicodeDecodeError). Here's a toy example (python 3):
>>> from io import BytesIO
>>> import pandas as pd
>>> csv = b'Encoding,Data\nascii,abc\nwindows-1252,\xae\nutf-8,\xe2\x80\x9c1\xe2\x80\x9d\n'
>>> pd.read_csv(BytesIO(csv))
Traceback (most recent call last):
File "pandas/_libs/parsers.pyx", line 1130, in pandas._libs.parsers.TextReader._convert_tokens
File "pandas/_libs/parsers.pyx", line 1254, in pandas._libs.parsers.TextReader._convert_with_dtype
File "pandas/_libs/parsers.pyx", line 1269, in pandas._libs.parsers.TextReader._string_convert
File "pandas/_libs/parsers.pyx", line 1459, in pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 0: invalid start byte
I would like a result equivalent to this:
>>> df = pd.DataFrame({'Encoding': ['ascii','windows-1252','utf-8'],
... 'Data': [b'abc',b'\xae',b'\xe2\x80\x9c1\xe2\x80\x9d']})
>>> df
Encoding Data
0 ascii b'abc'
1 windows-1252 b'\xae'
2 utf-8 b'\xe2\x80\x9c1\xe2\x80\x9d'
Which could (in this toy example) be further processed like this:
>>> df.apply(lambda row: str(row.Data,row.Encoding), axis=1)
0 abc
1 ®
2 “1”
dtype: object
I'd prefer solutions using only pandas, but I'm willing to look at other parsing libraries if absolutely necessary.