I'm processing a file csv file with pandas, so, I open it:
df = pd.read_csv(my_file, low_memory=False)
I'm applying some sanitizing functions, changing some strings to numbers, and then when I want to save the dataframe into a file I do this:
df.to_csv(output_file, index=False)
In some cases this throws a UnicodeEncodeError, so I want to know how to avoid this. I know there's an encoding parameter in the read_csv and the to_csv methods but whenever I have used it, it throws the error again.
I need to build a strong enough code that doesn't fail in the cases where the file has non-ascii characters. I know there's a parameter in the str.encode method, which is ignore and I would like to use something like that, but I'm not sure how to do it.
EDIT:
I know I can use encodings as latin1, iso-8859-1 or some others to make it work, but I would like the output file to be encoded in either ascii (preferably) or utf-8.