1

I am trying to read a csv file in python(jupyter notebook) but getting an error. Below is the code:

import sys
import pandas as pd
import scipy
import numpy as np
import seaborn as sns
import matplotlib
import sklearn
import matplotlib.pyplot as plt
pwd
data = pd.read_csv("C:\Users\DELL\Desktop\creditcard.csv")

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

https://i.stack.imgur.com/l6kl7.png

Edit: I've tried all of the following and still getting the same error.

data = pd.read_csv("C:\Users\DELL\Desktop\creditcard.csv")
data = pd.read_csv("C:\\Users\\DELL\\Desktop\\creditcard.csv")
data = pd.read_csv(r"C:\Users\DELL\Desktop\creditcard.csv")
data = pd.read_csv(r"C:\\Users\\DELL\\Desktop\\creditcard.csv")

data = pd.read_csv("C:/Users/DELL/Desktop/creditcard.csv")
data = pd.read_csv("C://Users//DELL//Desktop//creditcard.csv")
data = pd.read_csv(r"C:/Users/DELL/Desktop/creditcard.csv")
data = pd.read_csv(r"C://Users//DELL//Desktop//creditcard.csv")

Also, I've tried all of them with a single quote (') but its no use. Here's a picture of the file details: https://i.stack.imgur.com/NmeoB.png

Shreyansh Jain
  • 96
  • 1
  • 2
  • 8
  • @MayankPorwal I've tried this but still getting the same error. – Shreyansh Jain Sep 04 '19 at 09:55
  • The duplicate question does address and fix the problem *that you described*. If it still doesn't work, then either something went wrong in running the new code (e.g. make sure you saved the .py file before trying again) or there is a *new* problem that demands a new question and a *complete* stack trace specific to the new problem. You might *think* you get "the same error", but it could easily have changed. – Karl Knechtel Jul 07 '21 at 00:53

2 Answers2

2

You can try any of the following solutions:-

1: By putting r converts normal string to raw string:
    pandas.read_csv(r"C:\Users\DELL\Desktop\creditcard.csv")

2: Replacing with double backward-slash:
    pandas.read_csv("C:\\Users\\DELL\\Desktop\\creditcard.csv")

3: Replacing with forward-slash:    
    pandas.read_csv("C:/Users/DELL/Desktop/creditcard.csv")
Lovleen Kaur
  • 342
  • 2
  • 9
1

try this. will fix the issue.

data = pd.read_csv(r"C:\Users\DELL\Desktop\creditcard.csv.csv")
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Can you include an explanation of why and how this solves it, and why this is a better answer than the existing answers? – Dragonthoughts Nov 13 '19 at 14:10
  • 1
    why the .csv.csv? – B. Go Nov 13 '19 at 14:13
  • well i was trapped in the same scenerio as the above user. even tried all of them "/" "//" etc all of em as mentioned above. well in my case the problem i had was ,the name of the dataset itself was "creditcard.csv" so i did give it a try "/creditcard.csv.csv" it worked. well i know its not too technical but i just shared my solution.hope it worked. – karthik jeef Nov 14 '19 at 14:24