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 \
#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
Reply
#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'
Afy555 likes this post
Reply
#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?
Reply
#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.
[Image: XNaK6F.jpg]
Reply
#5
Yes, you're right! there is smth wrong with the path. I'll take a closer look at it.
Reply
#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?
Reply
#7
Sad 
(May-11-2018, 09:01 AM)Jack_Sparrow Wrote: 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


im also facing same problem sir...and im using google colab...plz help me
Reply
#8
train_dir = 'data/train'
val_dir = 'data/test'
train_datagen = ImageDataGenerator(rescale=1./255)
val_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory('C:\\Users\\Dell\\Desktop\\train',
target_size=(48,48),
batch_size=64,
color_mode="gray_framescale",
class_mode='categorical')

validation_generator = val_datagen.flow_from_directory('C:\\Users\\Dell\\Desktop\\test',
target_size=(48,48),
batch_size=64,
color_mode="gray_framescale",
class_mode='categorical')
its my code....and it raised some error....


ValueError Traceback (most recent call last)
<ipython-input-8-cec40955d457> in <module>()
8 batch_size=64,
9 color_mode="gray_framescale",
---> 10 class_mode='categorical')
11
12 validation_generator = val_datagen.flow_from_directory('C:\\Users\\Dell\\Desktop\\test',

3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/preprocessing/image.py in flow_from_directory(self, directory, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation)
956 follow_links=follow_links,
957 subset=subset,
--> 958 interpolation=interpolation)
959
960 def flow_from_dataframe(self,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/preprocessing/image.py in __init__(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype)
394 subset=subset,
395 interpolation=interpolation,
--> 396 **kwargs)
397
398

/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/directory_iterator.py in __init__(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype)
100 save_format,
101 subset,
--> 102 interpolation)
103 self.directory = directory
104 self.classes = classes

/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/iterator.py in set_processing_attrs(self, image_data_generator, target_size, color_mode, data_format, save_to_dir, save_prefix, save_format, subset, interpolation)
174 if color_mode not in {'rgb', 'rgba', 'grayscale'}:
175 raise ValueError('Invalid color mode:', color_mode,
--> 176 '; expected "rgb", "rgba", or "grayscale".')
177 self.color_mode = color_mode
178 self.data_format = data_format

ValueError: ('Invalid color mode:', 'gray_framescale', '; expected "rgb", "rgba", or "grayscale".')

....its an error i got plz hlp me
Reply
#9
Thumbs Up 
Thank you, works for me! Cool
Reply


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

Forum Jump:

User Panel Messages

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