2
$\begingroup$

I have DataFrame which has column with spanish regions, when I import the .csv as following

df = pd.read_csv('...',delimiter=';',decimal=',')

And print the content of column, I see this: Castilla y Le�n.

I could not find a way how to fix it, but I am assuming there is smart way how to do it.

$\endgroup$
3
$\begingroup$

read_csv has an optional argument called encoding that deals with the way your characters are encoded.

You can give a try to:

df = pandas.read_csv('...', delimiter = ';', decimal = ',', encoding = 'utf-8')

Otherwise, you have to check how your characters are encoded (It is one of them).

You can read the doc of read_csv here

| improve this answer | |
$\endgroup$
  • $\begingroup$ The reason why I didn't convert my comment to an answer is because I didn't have an example which could be helpful to the OP. So, pl add an example if you can :) $\endgroup$ – Dawny33 Jun 22 '16 at 16:19
  • $\begingroup$ I tried the encoding and with no success. In the end I solved it by changing the encoding on DB level, so it comes to Python clean. $\endgroup$ – HonzaB Jun 29 '16 at 8:39
2
$\begingroup$

The solution was posted in Stack Overflow

You need:

df.to_csv('file.csv',encoding='utf-8-sig')
| improve this answer | |
$\endgroup$
0
$\begingroup$

When using MacOs try this:

df.to_csv('file.csv', encoding='maccentraleurope')

From: Python Codecs

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.