[Python] Importing Data from Excel File in PyCharm

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I've been having some issues reading in data from an excel file using python. I'm using PyCharm, so I don't know if that may be the problem, but I've tried 3 or 4 tutorials and I haven't had any luck.

    This is what my code looks like:

    import pandas as pd

    data = pd.ExcelFile(r'C:\Users\lfasanello\PycharmProjects\excel2\sales.xlsx')
    print(data.sheet_names)


    import xlrd

    loc = r"C:/Users/lfasanello/PycharmProjects/excel2/sales.xlsx"
    wb = xlrd.open_workbook(loc)
    sheet = wb.sheet_by_index(0)

    print(sheet.cell_value(0, 0))


    In this code I'm trying two different methods of reading in the excel data, and neither are working. In the first section where I import pandas, I moved the sales.xlsx file into the working directory, so I don't think thats the problem. In the second attempt where I import xlrd, where I set loc = file path, I'm getting the error:

    SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

    I've tried some work-arounds like putting r in front of the file path like this: r"C:\Users\lfasanello\Desktop\sales.xlsx"

    I've also tried double back slashes, like this: "C:\\Users\\lfasanello\\Desktop\\sales.xlsx"

    But even when I don't get an error, the program isn't printing anything. If someone can help me out I'd greatly appreciate it, thanks.

    Continue reading...

Compartilhe esta Página