Jupyter中Python中的数据集加载错误


import pandas as pd
import numpy as ny
studentPerfomance = 'C:UsersVigneshDesktopprojectstudents-performance-in-examsStudentsPerformance.csv'

错误

File "<ipython-input-10-056bf84aaa71>", line 1
studentPerfomance = 'C:UsersVigneshDesktopprojectstudents-performance-in-examsStudentsPerformance.csv'
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape

使用标准斜杠/,而不是反斜杠。使用反斜杠分隔文件夹不是一种好的做法。我不知道为什么Windows仍然使用它作为显示路径的标准方式。

反斜杠的问题与转义序列有关,如n(新行(或t(制表符(。

因此,解决方案是用标准斜杠/替换als反斜杠。

import pandas as pd
import numpy as ny
studentPerfomance = 'C:/Users/Vignesh/Desktop/project/students-performance-in-exams/StudentsPerformance.csv'

问题是使用字符串作为路径。

只需将r放在普通字符串之前,它就可以将普通字符串转换为原始字符串:

studentPerfomance = r'C:UsersVigneshDesktopprojectstudents-performance-in-examsStudentsPerformance.csv'

studentPerfomance = 'C:\Users\Vignesh\Desktop\project\students-performance-in-exams\StudentsPerformance.csv'

总的来说,您所做的没有错。我也为你感到骄傲,因为你的道路上没有任何空间!(非常不专业(。问题是studentPerformance字符串中的反斜杠((是Python中的转义字符。因此Python每次看到时都会从字符串中转义。

也就是说,Windows在系统路径中使用反斜杠,而不是像基于Linux的操作系统那样使用正斜杠,这给用户带来了额外的痛苦。

解决此问题的最佳方法是在字符串前面加一个r,如下所示:

studentPerfomance = r'C:UsersVigneshDesktopprojectstudents-performance-in-examsStudentsPerformance.csv'

这个命令告诉Python忽略反斜线,这样它就不会对字符串进行转义。