1

I tried to open my csv files in jupyter windows 10 and it shows the error "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape". I have also went to other threads to find solution like df = pd.read_csv('~/Desktop/FullData.csv') and it says file does not exist even though it exists. Any suggestions?

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

df = pd.read_csv('C:\Users\kkang2\Desktop\FullData.csv')  #<--error here
5
  • Two things: 1) Is the case correct? Win is not case sensitive but python is. 2) If correct, open Explorer > Shift right-click “copy as path”. Paste this path into your notebook and compare for differences. Commented Mar 5, 2020 at 9:24
  • @S3DEV i replaced my original path with the one that i found using copy as path with this "\\fab2crp-nas1\home22\kkang2\Profile\Desktop\FullData.csv" and it still gives the file does not exist error... Commented Mar 5, 2020 at 9:31
  • 1
    Have you replaced the backslashes with forward slashes? //fab2crp-nas1/home22/kkang2/Profile/Desktop/FullData.csv Commented Mar 5, 2020 at 9:33
  • My pleasure. So there were two issues here: 1) the original path was incorrect and 2) the backslashes were acting as escape charaters. Glad it's working for you. Commented Mar 5, 2020 at 9:39
  • 1
    cena: I reverted your last edit. Please don't apply solution to code in your question because then it no longer makes sense to others reading it. Commented Mar 5, 2020 at 10:25

4 Answers 4

2

The backslashes are your issue - they are acting as escape characters.

Solution:

Use forward slashes like: 'C:/Users/kkang2/Desktop/FullData.csv'

Benefit:

Using forward slashes as above carries a benefit over double backslashes as this method is portable for both Win and *nix environments, when using relative paths.

Sign up to request clarification or add additional context in comments.

4 Comments

now it says" file does not exist error" when i have already saved my csv file on my desktop...
it says name "os is not defined"
it says false..which means it does not exist?
But why is this so ? I have my csv file save on my desktop?
1

rewrite the path with two slashes

'C:\\Users\\kkang2\\Desktop\\FullData.csv'

2 Comments

@ Никита Колесников what do you mean by 2 slashes?
back double slash, example - "\\path"
0

Try this: Use 'r' before providing path to the pd.read_csv method.

df = pd.read_csv(r'C:/Users/kkang2/Desktop/FullData.csv') 

'r' stands for 'raw'. Please look over the internet if you are more interested.

Comments

0

I prefer using the os package

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import os

dirname = 'C:\Users\kkang2\Desktop'
filename = 'FullData.csv'
df = pd.read_csv(os.path.join(dirname, filename))

With os even the backslash works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.