-1

I have this code.

import pandas as pd 
df = pd.read_csv(r"C:\Users\Hp\Desktop\Stock_Data_Set.csv")
df.head()

I am trying to read a csv file but when I run this code,I get no error and it stops running.If i remove the r,then it gives a unicode error that is

  File "c:\Users\Hp\Desktop\test.py", line 2
    df = pd.read_csv("C:\Users\Hp\Desktop\Stock_Data_Set.csv")
                     ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 
escape
Mr.R-Word
  • 23
  • 8
  • 1
    https://stackoverflow.com/questions/37400974/unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3-trunca –  Oct 30 '20 at 13:40
  • Does this answer your question? [Windows path in Python](https://stackoverflow.com/questions/2953834/windows-path-in-python) – BigBen Oct 30 '20 at 13:48

2 Answers2

0

The error is caused by the interpretation of escape characters (any sequence starting with ''). In particular, "\U" introduces a 32-bit unicode character

You should use a raw string to avoid such behaviour (r"xxx") See https://docs.python.org/3.6/reference/lexical_analysis.html, 2.4.1. String and Bytes literals both escape characters and raw strings.

0

Also without raw strings, you can escape \ with another backslash:

df = pd.read_csv("C:\\Users\\Hp\\Desktop\\Stock_Data_Set.csv")
Wasif
  • 14,755
  • 3
  • 14
  • 34