Not able to read read my UNRATE.csv file into pandas

screen link

My Code

import pandas as pd
unrate = pd.read_csv("C:\Users\brayan opiyo\Downloads\UNRATE.csv",encoding = "latin-1")
urate["DATE"] = pd.to_datetime(unrate["DATE"])
unrate.head()
​

What I expected to happen:

         DATE  VALUE
0  1948-01-01    3.4
1  1948-02-01    3.8
2  1948-03-01    4.0
3  1948-04-01    3.9
4  1948-05-01    3.5

What actually happened:

  File "<ipython-input-2-290333855c16>", line 2
    unrate = pd.read_csv("C:\Users\brayan opiyo\Downloads\UNRATE.csv",encoding = "latin-1")
                        ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

how can I solve out the problem, please guys help.

Hi @brayanopiyo18

First, your screen link doesn’t work because it looks like you are working on your machine, a screen cap would have been better

However the error happens because the first backslash in your string is being recognized as an special character and the error says that actually recognize it as an unicodeescape because of the "U".

So you can do a couple of things

  1. Add another backslash to tell python and pandas to ignore the special character
unrate = pd.read_csv("C:\\Users\\brayan opiyo\\Downloads\\UNRATE.csv",encoding = "latin-1")
  1. Change backslash to slash
unrate = pd.read_csv("C:/Users/brayan opiyo/Downloads/UNRATE.csv",encoding = "latin-1")
  1. Write r before the string to transform it into a raw string, basically does the same as the double backslahs, tells python and pandas to ignore special characters
unrate = pd.read_csv(r"C:\Users\brayan opiyo\Downloads\UNRATE.csv",encoding = "latin-1")

This python documentation can help you to understand more about it
https://docs.python.org/3/reference/lexical_analysis.html#literals

Good luck!

4 Likes

@alegiraldo666
wow! it has worked perfectly just by adding r.Thanks alot for the perfect idea.

Hi @brayanopiyo18 , if Alejandro’s answer helped you then please mark it as a solution for the benefit of the rest of the community as well.

2 Likes

@the_doctor well noted.