TransWikia.com

Pandas - read CSV with spanish characters

Data Science Asked by HonzaB on August 18, 2021

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.

4 Answers

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

Answered by Igor OA on August 18, 2021

The solution was posted in Stack Overflow

You need:

df.to_csv('file.csv',encoding='utf-8-sig')

Answered by renakre on August 18, 2021

When using MacOs try this:

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

From: Python Codecs

Answered by sanwall on August 18, 2021

I have a Spanish dataframe and I use the latin-1 encoding.

df = pd.read_csv('...', delimiter = ';', decimal = ',', encoding = 'latin-1')

Answered by Hans David Santiago Villabon on August 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP