Error Writing Pandas To Csv With Euro Sign Delimiter
I'm trying to write a Pandas data frame to a csv with '€' as a delimiter. data.to_csv(file_path, sep = '€') however, I get the error: TypeError: 'delimiter' must be an 1-chara
Solution 1:
Time to switch to python 3.
This is because by default string is ascii in python 2. ASCII characters are 1 byte (hence their range is 0 to 127). This is not sufficient to represent all characters; in this case '€'. To represent this 3 bytes is needed.
Hence the world switched to unicode. Which has higher range. Luckily python3 uses unicode for all its string.
In python3 data.to_csv(file_path, sep = '€')
will work properly.
Solution 2:
You can use an encoding that uses only one byte to encode '€', like iso8859-15 or cp1252:
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]])
sep = u'€'.encode('iso8859-15')
result = df.to_csv(sep=sep)
print result.decode('iso8859-15')
€0€1€2
0€1€2€3
1€4€5€6
Note that this won't work in python 3, where sep
must be a unicode string.
Solution 3:
I had the same issue in Python3.
This code works for me:
df = pd.read_csv('', sep='‰', engine='python', encoding='utf-8')
Post a Comment for "Error Writing Pandas To Csv With Euro Sign Delimiter"