SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 12-13: malformed N character escape
Question:
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(".verikumesiNBA_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(“.verikumesiNBA_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?
Answers:
".verikumesiNBA_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".verikumesiNBA_player_of_the_week.csv"
".\verikumesi\NBA_player_of_the_week.csv"
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(".verikumesiNBA_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(“.verikumesiNBA_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?
".verikumesiNBA_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".verikumesiNBA_player_of_the_week.csv"
".\verikumesi\NBA_player_of_the_week.csv"