0

As title says, I'm having issues importing excel files that are on my computer into Python. FYI I'm relatively new at all this, so I might just outright say some things that are wrong. Anyways, I'm using Pycharm as my IDE, and this is what I've attempted thus far:

import pandas as pd 
fileLocation = "C:\Users\Dcwahl\Desktop\New folder\Diego\DATA 2017"
fileName = 'data_paretos.xlsx'
data = pd.ExcelFile(fileLocation + fileName)
print(data.sheet_names)

which gives me the following error:

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

Any thoughts on what I'm doing wrong? Thanks a ton.

Diego Wahl
  • 3
  • 1
  • 2
  • One possible issue is that `fileLocation + fileName = 'C:\Users\Dcwahl\Desktop\New folder\Diego\DATA 2017data_paretos.xlsx'` - you need a backslash after `DATA 2017`. – Daniel R. Livingston Jun 14 '17 at 18:07
  • Possible duplicate of [(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape](https://stackoverflow.com/questions/37400974/unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3-trunca) – KenHBS Jun 14 '17 at 18:50

3 Answers3

1

There is a small mistake in the code.when you give the file location you have to make it

fileLocation = "C:\Users\Dcwahl\Desktop\New folder\Diego\DATA 2017\"
fileName = 'data_paretos.xlsx'

this is because when you call the method pd.ExcelFile(fileLocation + fileName) the filenamelocation+filename is returning - C:\Users\Dcwahl\Desktop\New folder\Diego\DATA 2017data_paretos.xlsx instead of C:\Users\Dcwahl\Desktop\New folder\Diego\DATA 2017\data_paretos.xlsx

Let me know if the problem still persists. Happy coding!

1

Another issue is the fact that python thinks you are trying to use an escape character after every slash. it says

'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

\UXXXXXXXX is the C:\Users....So in addition to adding a trailing \ you must add a \ in front of all your \'s. Like so...

fileLocation = "C:\\Users\\Dcwahl\\Desktop\\New folder\\Diego\\DATA 2017\\"
mrob
  • 61
  • 3
0

The parameter you're passing to pd.ExcelFile is C:\Users\Dcwahl\Desktop\New folder\Diego\DATA 2017data_paretos.xlsx. You'll want to add another slash after your directory name.

Ben Schroeder
  • 346
  • 2
  • 7