Python Forum
(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \
HI there,
I have follow problem:
import pandas as pd

subway_df = pd.read_csv('C:\Users\DSule\Desktop\python\nyc.csv')
 File "<ipython-input-25-80cb9d4c408e>", line 3
    subway_df = pd.read_csv('C:\Users\DSule\Desktop\python\nyc.csv')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
I tried to solve this problem with three different solutions:
- \\
- r
- "nyc.csv"

But nothing works for me... Confused
I know, there are already similiar questions in this forum. But if you take a closer look to this questions, they are not answered Dodgy
Feel frustraited Cry

I use Python 3
And I installed Jupyter with anaconda
Never use use single backslash (\) that way in path,because of escape character.
\U starts an eight-character Unicode escape in Python 3.
>>> s = 'C:\Users\DSule\Desktop\python\nyc.csv' Traceback (most recent call last): ......... SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Here are three ways that work.
>>> # Raw string(r)
>>> s = r'C:\Users\DSule\Desktop\python\nyc.csv'
>>> s

>>> # Other way /
>>> s = 'C:/User/DSule/Desktop/python/nyc.csv'
>>> s

>>> # Double up
>>> s = 'C:\\Users\\DSule\\Desktop\\python\\nyc.csv'
>>> s
Thank you!
I tried all of them. Here are results:

import pandas as pd
subway_df = pd.read_csv(r'C:\Users\DSule\Desktop\python\nyc.csv')
NO output/No reaction
import pandas as pd
subway_df = pd.read_csv('C:/User/DSule/Desktop/python/nyc.csv')
File b'C:/User/DSule/Desktop/python/nyc.csv' does not exist
#Input 3
import pandas as pd
subway_df = pd.read_csv('C:\\Users\\DSule\\Desktop\\python\\nyc.csv')
#Output 3
NO output/No reaction
Any other ideas?
Check your path Path and file.

Here a little basic about Path and file as i think you struggle with that.
Navigate training in cmd and check file,this way you now that path is correct.
Then open file Pandas.
Microsoft Windows [Version 10.0.16299.431]
(c) 2017 Microsoft Corporation. Med enerett.

# Navigate with cd
C:\>cd foo1

# look at files in folder
 Volume in drive C has no label.
 Volume Serial Number is EED7-45CC

 Directory of C:\foo1

11.05.2018  12:46    <DIR>          .
11.05.2018  12:46    <DIR>          ..
13.09.2017  22:03                 0 cd1.txt
11.05.2018  12:40                73 my_csv.csv
               2 File(s)             73 bytes
               2 Dir(s)  66 910 232 576 bytes free

# Open file in folder
C:\foo1>type my_csv.csv
cmd Anaconda:
Python 3.6.5 |Anaconda custom (64-bit)| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> df = pd.read_csv(r'C:\foo1\my_csv.csv')
>>> df
  Student  Age    Subject
0    Adam   13      Maths
1    Alex   14    English
2   Shawn   15  Geography
Then it also work the same in Jupyter NoteBook.
[Image: XNaK6F.jpg]
Yes, you're right! there is smth wrong with the path. I'll take a closer look at it.
Hi Jack_Sparrow. i did not see your resolution to this issue. I followed all the steps noted above including r, \\, /
none worked similar to you.
then i used cmd to verify the path and the file. there were no issues as i was able to locate and open the file with cmd.
used that path in Jupyter and still get Syntax Error.
Can you provide any additional assistance or thought on this?

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fatal Python error: initfsencoding: unable to load the file system codec gauravbhardwajee 12 17,707 Apr-30-2020, 07:45 PM
Last Post: barrpath

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020