Python – Pandas dataframe to excel gives “file is not UTF-8 encoded”

dataframepandaspythonutf-8

I'm working on lists that I want to export into an Excel file.

I found a lot of people advising to use pandas.dataframe so that's what I did. I could create the dataframe but when I try to export it to Excel, the file is empty, there is just the following message:

" Error! [file_pathway] is not UTF-8 encoded. Saving disabled. See console for more details".

I didn't see any more info on the console, and every example I found on the Internet leads to the same error message.

The different lists I'm using contain different types of data. So I try to convert every element I could into a UTF-8 encoded element. I couldn't do it for the "float" list nor for the "NoType" list.

After that, here is what I wrote :

d = {'Dataset_name': dataset_names, 'Parameter_name': para_names, 'Parameter_amount': para_amounts, 'Parameter_unit': para_units, 'Parameter_variable': para_variables, 'Parameter_formula': para_formulas}
df = pd.DataFrame(data=d)

from pandas import ExcelWriter
writer = ExcelWriter('Ocelot_Export.xlsx')
df.to_excel(writer, encoding='utf8', index=False)
writer.save()

The dataframe is correct, as I can print it in Jupyter Notebook. The only problem is the exportation. Please let me know if you have any idea about what's wrong.

Best Answer

The cause of this problem is likely just that jupyter is unable to display .xlsx files. Try downloading the file from jupyter (checkbox next to the file name -> "Download" button near the page header) onto your local machine and open it using excel.

2020 Update: If you're using Jupyter Lab, jupyterlab-spreadsheet is a great way to view excel files without leaving your browser.