'How read a csv file from my computer using pandas
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')
Solution 1:[1]
try C:/Users/pc/Desktop/Work/flight.csv or escape C:\\Users\\pc\\Desktop\\Work\\flight.csvotherwise \ is interpreted as escape sequence.
Solution 2:[2]
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.
Solution 3:[3]
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')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dyno Fu |
| Solution 2 | Erik Wessel |
| Solution 3 | SM1312 |
