Solve the problem of reading files by jupyter of python3
- 2021-10-15 10:53:14
- OfStack
1. Error occurred
train_df = pd.read_csv( 'C:\Users\lenovo\Desktop\train.csv',encoding='utf-8')
Error reporting:
File " < ipython-input-45-27822e1fee69 > " , line 1
train_df = pd.read_csv('C:\Users\lenovo\Desktop\train.csv',encoding='utf-8') ^
SyntaxError : (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
2. Solutions
Prefix the file name with r
train_df = pd.read_csv( r'C:\Users\lenovo\Desktop\train.csv',encoding='utf-8')
Add: Python Three Common Problems When Reading Files with Absolute Path
1. The catalogue is in Chinese
Such as writing directly
f=open('Users/librom/Documents/Python/ Temporary file / Analog volume / Dragon 8 Ministry .py ','r')
You will report an error
Traceback (most recent call last):
File " < pyshell#24 > ", line 1, in < module >
f=open ('Users/librom/Documents/Python/Temporary File/Analog Volume/Tianlong 8. py', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'Users/librom/Documents/Python/Temporary File/Analog Volume/Tianlong 8. py'
2 Solutions
1. Change all Chinese to English
2. Change the current working directory to the specified path
import os
os.chdir(r'/Users/librom/Documents/ Temporary file / Analog volume ')
2. When viewing the file path with mac terminal,
If the file name of a file has a space between the text, it will automatically become a backslash bar with a space when it is displayed in the middle.
3. Under window, path 1 is generally\
In the path search of python, you need/, so you need to pay attention to conversion. In addition, it can be changed to\. (In python,\ means an escape character, but\ means a backslash bar.)
Current python version 3.7. 4