1

I am trying to open a CSV file using pandas in Jupyter notebook. Unfortunately, I am getting syntax error.

I tried to google the issue but nothing worked out. I tried to change the path backslash front slash, but no use.

import pandas as pd
data = pd.read_csv(‪"C:\Users\EYKIM\Desktop\Advertising.csv")

I should get into next line but getting the following error.

File "", line 2 data = pd.read_csv(‪"C:\Users\EYKIM\Desktop\Advertising.csv") ^ SyntaxError: invalid character in identifier

enter image description here

alluri
  • 55
  • 1
  • 8

3 Answers3

2

Backslashes in a string are the start of an escape sequence. You either need to double the backslashes to escape them, use a raw string, or change them to forward slashes.

data = pd.read_csv(‪"C:\\Users\\EYKIM\\Desktop\\Advertising.csv")
data = pd.read_csv(‪r"C:\Users\EYKIM\Desktop\Advertising.csv")
data = pd.read_csv(‪"C:/Users/EYKIM/Desktop/Advertising.csv")
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This worked fine. Thank you dude. `data = pd.read_csv("C:\\Users\\EYKIM\\Desktop\\Advertising.csv")` – alluri Sep 06 '19 at 07:56
1

Try defining the path starting from home. You can find it by running in the folder that your csv is, pwd in the terminal or by pressing Ctrl+L while in in the folder.

Of course don't forget to add the file at the end of the path

Charalamm
  • 1,547
  • 1
  • 11
  • 27
  • Thank you for the response and your time. It was the error of the single backslash. A double backslash in the path worked fine. Cheers !! – alluri Sep 06 '19 at 07:54
1

You need to change it to:

import pandas as pd
data = pd.read_csv(‪"C:/Users/EYKIM/Desktop/Advertising.csv")

or

data = pd.read_csv(‪"C:\\Users\\EYKIM\\Desktop\\Advertising.csv")

or

data = pd.read_csv(‪r"C:\Users\EYKIM\Desktop\Advertising.csv")
PV8
  • 5,799
  • 7
  • 43
  • 87