3

I have a very simple error in Python with Spyder:

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt 

ds=pd.read_csv(".\verikumesi\NBA_player_of_the_week.csv")

When I run the above code, the I get an error:

File "C:/Users/Acer/Desktop/MASAÜSTÜ/github/deneme.py", line 12 ds=pd.read_csv(".\verikumesi\NBA_player_of_the_week.csv") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 12-13: malformed \N character escape

How can I fix it?

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
Ali ÜSTÜNEL
  • 183
  • 2
  • 9
  • Possible duplicate of [Python escaping backslash](https://stackoverflow.com/q/31213556/608639) and [How to print backslash with Python?](https://stackoverflow.com/q/19095796/608639). They discuss escaping backslashes, too. – jww Nov 28 '18 at 00:06
  • See also: [Windows path in Python](https://stackoverflow.com/questions/2953834/windows-path-in-python) – Karl Knechtel Aug 07 '22 at 03:31

1 Answers1

5
".\verikumesi\NBA_player_of_the_week.csv"

is invalid Python. In normal (non-raw) strings, the backslash combines with the following character to form an "character escape sequence", which mean something quite different. For example, "\n" means a newline character. There is no escape sequence "\N", and you don't want an escape sequence anyway, you want a backslash and a "N". One solution is to use raw strings (r"..."), which strip the backslash of its superpower. The other is to use a character escape sequence whose meaning is the backslash (\\).

tl;dr: Use either of these options:

r".\verikumesi\NBA_player_of_the_week.csv"
".\\verikumesi\\NBA_player_of_the_week.csv"
Amadan
  • 191,408
  • 23
  • 240
  • 301