Bottom Page

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 \
#1
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
Quote
#2
Never use use single backslash (\) that way in path,because of escape character.
\U starts an eight-character Unicode escape in Python 3.
Error:
>>> 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
'C:\\Users\\DSule\\Desktop\\python\\nyc.csv'

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

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

#Input1: 
import pandas as pd
subway_df = pd.read_csv(r'C:\Users\DSule\Desktop\python\nyc.csv')
#Output1
NO output/No reaction
#Input2
import pandas as pd
subway_df = pd.read_csv('C:/User/DSule/Desktop/python/nyc.csv')
#Output2
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?
Quote
#4
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.
cmd:
Microsoft Windows [Version 10.0.16299.431]
(c) 2017 Microsoft Corporation. Med enerett.

# Navigate with cd
C:\Windows\System32>cd\
C:\>cd foo1

# look at files in folder
C:\foo1>dir
 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
Student,Age,Subject
Adam,13,Maths
Alex,14,English
Shawn,15,Geography
cmd Anaconda:
G:\Anaconda3>python
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.
İmage
Jack_Sparrow likes this post
Quote
#5
Yes, you're right! there is smth wrong with the path. I'll take a closer look at it.
Quote
#6
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?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Fatal Python error: initfsencoding: unable to load the file system codec gauravbhardwajee 11 7,163 Dec-03-2018, 08:29 PM
Last Post: gauravbhardwajee

Forum Jump:


Users browsing this thread: 1 Guest(s)