Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load CSV file in Jupyter Notebook?

I'm new and studying machine learning. I stumble upon a tutorial I found online and I'd like to make the program work so I'll get a better understanding. However, I'm getting problems about loading the CSV File into the Jupyter Notebook.

I get this error:

File "<ipython-input-2-70e07fb5b537>", line 2
    student_data = pd.read_csv("C:\Users\xxxx\Desktop\student-intervention- 
system\student-data.csv")
                          ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in 
position 2-3: truncated \UXXXXXXXX escape

and here is the code: enter image description here

I followed tutorials online regarding this error but none worked. Does anyone know how to fix it?

3rd attempt with r"path" enter image description here

I've tried also "\" and utf-8 but none worked.

I'm using the latest version of Anaconda Windows 7 Python 3.7

like image 975
stella Avatar asked Mar 17 '19 17:03

stella


People also ask

How do I import a CSV file into Jupyter notebook VS code?

This is how you do it in windows. Step 1: Import pandas in your notebook. Step 3: Select the file –> Click on Home –> Click on Copy file path. Step 4: Use the file path to read the CSV file.


6 Answers

Use raw string notation for your Windows path. In python '\' have meaning in python. Try instead do string like this r"path":

student_data = pd.read_csv(r"C:\Users\xxxx\Desktop\student-intervention- system\student-data.csv")

If it doesnt work try this way:

import os

path = os.path.join('c:' + os.sep, 'Users', 'xxxx', 'Desktop', 'student-intervention-system', 'student-data.csv')
student_data = pd.read_csv(path)
like image 200
Bohun Mielecki Avatar answered Oct 08 '22 10:10

Bohun Mielecki


Try changing \ to /: -

import pandas as pd
student_data = pd.read_csv("C:/Users/xxxx/Desktop/student-intervention- 
system/student-data.csv")
print(student data)

OR

import pandas as pd

student_data = pd.read_csv("C:/Users/xxxx/Desktop/student-intervention- system/student-data.csv"r)

print(student data)
like image 26
L Prathyusha Avatar answered Oct 08 '22 08:10

L Prathyusha


Either replace all backslashes \ with frontslashes / or place a r before your filepath string to avoid this error. It is not a matter of your folder name being too long.


As Bohun Mielecki mentioned, the \ character which is typically used to denote file structure in Windows has a different function when written within a string.

From Python3 Documentation: The backslash \ character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

How this particularly affects your statement is that in the line

student_data = pd.read_csv("C:\Users\xxxx\Desktop\student-intervention- 
system\student-data.csv")

\Users matches the escape sequence \Uxxxxxxxx whereby xxxxxxxx refers to a Character with 32-bit hex value xxxxxxxx. Because of this, Python tries to find a 32-bit hex value. However as the -sers from Users doesn't match the xxxxxxxx format, you get the error:

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


The reason why your code works now is that you have placed a r in front of 'C:\Users\xxxx\Desktop\project\student-data.csv'. This tells python not to process the backslash character / as it usually does and read the whole string as-is.

I hope this helps you better understand your problem. If you need any more clarification, do let me know.

Source: Python 3 Documentation

like image 22
evantkchong Avatar answered Oct 08 '22 09:10

evantkchong


I had the same problem. I tried to encode it with 'Latin-1' and it worked for me.

    autos = pd.read_csv('filename',encoding = "Latin-1")
like image 25
Yuwe711 Avatar answered Oct 08 '22 09:10

Yuwe711


Try this student_data = pd.read_csv("C:/Users/xxxx/Desktop/student-intervention- system/student-data.csv").

Replacing backslashes in that code it will work for you.

like image 42
Qasim ali shah Avatar answered Oct 08 '22 09:10

Qasim ali shah


You probably have a file name with a backlash... try to write the path using two backlashes instead of one.

student_data = pd.read_csv("C:\\Users\\xxxx\\Desktop\\student-intervention-system\\student-data.csv")
like image 41
samology Avatar answered Oct 08 '22 08:10

samology