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
  • 34,372
  • 28
  • 129
  • 206
Ali ÜSTÜNEL
  • 143
  • 1
  • 7
  • 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

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
  • 179,482
  • 20
  • 216
  • 275