'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.
Solution 1:[1]
Seems like you need to open your command prompt, activate the environment you are using and type: pip install openpyxl
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MichaelB |
| Solution 2 | Brockenspook |
