I'm trying to read a CSV file that I have on my computer in a Jupyter Notebook. I using Pandas pd.read_csv(file path) but I'm getting this error:
File "C:\Users\pc\AppData\Local\Temp/ipykernel_15328/2333079912.py", line 1
flight_df=pd.read_csv('C:\Users\pc\Desktop\Work\flight.csv')
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3:
truncated \UXXXXXXXX escape
Here is my code so far:
#Calling Libraries
import numpy as np
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
flight_df=pd.read_csv('C:\Users\pc\Desktop\Work\flight.csv')
CodePudding user response:
try C:/Users/pc/Desktop/Work/flight.csv
or escape C:\\Users\\pc\\Desktop\\Work\\flight.csv
otherwise \
is interpreted as escape sequence.
CodePudding user response:
If you change the string to either contain double backslashes \\
as directory separators or put a r
in front of it like
flight_df=pd.read_csv(r'C:\Users\pc\Desktop\Work\flight.csv')
the loading of the file should succeed.
As an addition, the error regards the escaping of characters like \U
in C:\Users
.
CodePudding user response:
Its because your path as treated as normal string. You can do this to fix your issue:
flight_df = pd.read_csv(r'C:\Users\pc\Desktop\Work\flight.csv')