I have a simple script that converts values from one type to another using str.replace
. It works fine for values using standard English characters but it seems confused by Spanish characters like
ñ.
Example:
df['Best time to call?'] = df['Best time to call?']\
.str.replace('Afternoons / La tarde','afternoon')\
.str.replace('Evenings / La noche','evening')\
.str.replace('Do Not Call / No llamar','')\
.str.replace('Morning / La mañana','morning')
The converted values are output to a CSV through pandas. When I open the CSV in Excel, 'Morning / La mañana'
has been converted to 'Morning / La ma�ana'
even though all the other ones worked.
EDIT
The older question you all have suggested was specific to python 2 and I am using python 3.
I have found that preemptively replacing the mojibake solves the issue.
df['Best time to call?'] = df['Best time to call?'].str.replace('�', 'n')
followed by:
df['Best time to call?'] = df['Best time to call?'.str.replace('Morning / La manana','morning')
Not the most elegant solution, but it will do for now.