Search code examples
pandasdataframeuploadgoogle-colaboratoryxlsx

Unable to load an .xlsx file from my computer to Google Colab


I tried a few methods that I read online, but none seem to work. I have the file locally on my machine in a xlsx form and tried the following code:

import pandas as pd
import io

from google.colab import files
uploaded = files.upload()

Then I uploaded the file succesfully and when I tried to read it into a df, I got the following error:

df = pd.read_excel(io.StringIO(uploaded['File_Name.xlsx']))

TypeError Traceback (most recent call last) in () ----> 1 df = pd.read_excel(io.StringIO(uploaded['File_Name.xlsx']))

TypeError: initial_value must be str or None, not bytes

Any idea how to solve this? Thank you!


Solution

  • After uploading the file you can use it like this:

    df = pd.read_excel(uploaded['File_Name.xlsx'])
    

    Pandas read_excel can directly use bytes, there's no need to wrap it in io.BytesIO(uploaded['File_Name.xlsx']) (which would also work, but as xlsx is a binary file read_excel needs BytesIO instead of StringIO).