简体   繁体   中英

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')

try C:/Users/pc/Desktop/Work/flight.csv or escape C:\\Users\\pc\\Desktop\\Work\\flight.csv otherwise \ is interpreted as escape sequence.

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 .

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM