Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas dataframe to excel gives "file is not UTF-8 encoded"

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.

like image 428
STL Avatar asked Feb 24 '18 21:02

STL


People also ask

What does it mean when a file is not UTF-8 encoded?

This error is created when the uploaded file is not in a UTF-8 format. UTF-8 is the dominant character encoding format on the World Wide Web. This error occurs because the software you are using saves the file in a different type of encoding, such as ISO-8859, instead of UTF-8.

How can a DataFrame be exported to a CSV file with encoding as UTF-8 after hiding index and header labels?

How to write a pandas DataFrame to CSV file. Delimit by a tab you can use the sep argument . Avoid index column use index argument . Use a specific encoding (e.g. 'utf-8' ) use the encoding argument .


1 Answers

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.

like image 97
Battery_Al Avatar answered Sep 29 '22 04:09

Battery_Al