简体   繁体   中英

How to import an Excel file to Python? [New to Python]

I'm currently using the following code (as learned in the DataCamp courses I followed):

import pandas as pd

pd.read_excel('C:\Users\jeenb\OneDrive\Bureaublad\Master Finance\Master Thesis\Input Python\Training Set.xlsx')

I'm getting the following error message:

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

I've tried the following as was suggested in this forum before:

pd.read_excel('C:\\Users\\jeenb\\OneDrive\\Bureaublad\\Master Finance\\Master Thesis\\Input Python\\Training Set.xlsx')

pd.read_excel(r'C:\Users\jeenb\OneDrive\Bureaublad\Master Finance\Master Thesis\Input Python\Training Set.xlsx')

pd.read_excel(r"C:\Users\jeenb\OneDrive\Bureaublad\Master Finance\Master Thesis\Input Python\Training Set.xlsx")

All of these give the following error message:

Traceback (most recent call last):
  File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\compat\_optional.py", line 126, in import_optional_dependency
    module = importlib.import_module(name)
  File "C:\Program Files\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'openpyxl'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    pd.read_excel(r"C:\Users\jeenb\OneDrive\Bureaublad\Master Finance\Master Thesis\Input Python\Training Set.xlsx")
  File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\io\excel\_base.py", line 457, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\io\excel\_base.py", line 1419, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
  File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\io\excel\_openpyxl.py", line 524, in __init__
    import_optional_dependency("openpyxl")
  File "C:\Users\jeenb\AppData\Roaming\Python\Python310\site-packages\pandas\compat\_optional.py", line 129, in import_optional_dependency
    raise ImportError(msg)
ImportError: Missing optional dependency 'openpyxl'.  Use pip or conda to install openpyxl.

I'm typing this code in the IDLE Shell 3.10.2. Previously I got an error message while importing the pandas however since I don't get this anymore I feel like I fixed this.

似乎您需要打开命令提示符,激活您正在使用的环境并输入: pip install openpyxl

I found the answer. Thought I would post it if anyone in the future would be struggeling with this and found this post. My slashes were facing the wrong way, just changing \ to / fixed the problem.

In my case of using pycharm, I needed to go to File -> settings -> Project: projectName -> Python interpreter. Then click on + button will show you a pop up window to install packages. Then you can search for openpyxl and get the latest package. Then click Install package to install it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM