Python pandas.read_csv FileNotFoundError:文件 b'\xe2\x80\xaa<etc>' 尽管路径正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42165649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas.read_csv FileNotFoundError: File b'\xe2\x80\xaa<etc>' despite correct path
提问by Impuls3H
I'm trying to load a .csv
file using the pd.read_csv()
function when I get an error despite the file path being correct and using raw strings.
尽管文件路径正确且使用原始字符串,但出现错误时,我仍尝试.csv
使用该pd.read_csv()
函数加载文件。
import pandas as pd
df = pd.read_csv('?C:\Users\user\Desktop\datafile.csv')
df = pd.read_csv(r'?C:\Users\user\Desktop\datafile.csv')
df = pd.read_csv('C:/Users/user/Desktop/datafile.csv')
all gives the error below:
全部给出以下错误:
FileNotFoundError: File b'\xe2\x80\xaaC:/Users/user/Desktop/tutorial.csv' (or the relevant path) does not exist.
FileNotFoundError:文件 b'\xe2\x80\xaaC:/Users/user/Desktop/tutorial.csv'(或相关路径)不存在。
Only when i copy the file into the working directory will it load correct.
只有当我将文件复制到工作目录时,它才会正确加载。
Is anyone aware of what might be causing the error?
有没有人知道可能导致错误的原因?
I had previously loaded other datasets with full filepaths without any problems and I'm currently only encountering issues since I've re-installed my python (via Anaconda package installer).
我之前已经加载了其他具有完整文件路径的数据集,没有任何问题,而且我目前只遇到问题,因为我重新安装了我的 python(通过 Anaconda 包安装程序)。
Edit:
I've found the issue that was causing the problem.
When I was copying the filepath over from the file properties window, I unwittingly copied another character that seems invisible.
Assigning that copied string
also gives an unicode error.
编辑:
我发现了导致问题的问题。
当我从文件属性窗口复制文件路径时,我无意中复制了另一个似乎不可见的字符。
分配复制的string
也会出现 unicode 错误。
Deleting that invisible character made any of above code work.
删除那个不可见的字符可以使上述任何代码工作。
采纳答案by Impuls3H
For my particular issue, the failure to load the file correctly was due to an "invisible" character that was introduced when I copied the filepath from the security tab of the file properties in windows.
对于我的特定问题,无法正确加载文件是由于我从 Windows 中文件属性的安全选项卡复制文件路径时引入的“不可见”字符。
This character is e2 80 aa
, the UTF-8 encoding of U+202A, the left-to-right embedding symbol. It can be easily removed by erasing (hitting backspace or delete) when you've located the character (leftmost char in the string).
这个字符是e2 80 aa
,U+202A的UTF-8编码,从左到右的嵌入符号。当您找到字符(字符串中最左边的字符)时,可以通过擦除(按退格或删除)轻松删除它。
Note: I chose to answer because the answers here do not answer my question and I believe a few folks (as seen in the comments) might meet the same situation as I did. There also seems to be new answers every now and then since I did not mark this question as resolved.
注意:我选择回答是因为这里的答案没有回答我的问题,我相信有些人(如评论中所见)可能会遇到与我相同的情况。由于我没有将此问题标记为已解决,因此似乎时不时也会有新的答案。
回答by WaterRocket8236
Try this and see if it works. This is independent of the path you provide.
试试这个,看看它是否有效。这与您提供的路径无关。
pd.read_csv(r'C:\Users\aiLab\Desktop\example.csv')
Here r
is a special character and means raw string. So prefix it to your string literal.
这r
是一个特殊字符,表示原始字符串。因此,将其作为字符串文字的前缀。
https://www.journaldev.com/23598/python-raw-string:
https://www.journaldev.com/23598/python-raw-string:
Python raw string is created by prefixing a string literal with ‘r' or ‘R'. Python raw string treats backslash () as a literal character. This is useful when we want to have a string that contains backslash and don't want it to be treated as an escape character.
Python 原始字符串是通过在字符串文字前加上 'r' 或 'R' 来创建的。Python 原始字符串将反斜杠 () 视为文字字符。当我们想要一个包含反斜杠的字符串并且不希望它被视为转义字符时,这很有用。
回答by hertopnerd
$10 says your file path is correct with respect to the location of the .py file, but incorrect with respect to the location from which you call python
$10 表示您的文件路径相对于 .py 文件的位置是正确的,但相对于您调用 python 的位置是不正确的
For example, let's say script.py is located in ~/script/, and file.csv is located in ~/. Let's say script.py contains
例如,假设 script.py 位于 ~/script/ 中,file.csv 位于 ~/ 中。假设 script.py 包含
import pandas
df = pandas.read_csv('../file.csv') # correct path from ~/script/ where script.py resides
If from ~/ you run python script/script.py
, you will get the FileNotFound error. However, if from ~/script/ you run python script.py
, it will work.
如果从 ~/ 运行python script/script.py
,您将收到 FileNotFound 错误。但是,如果从 ~/script/ 运行python script.py
,它将起作用。
回答by Waqar Khan
I know following is a silly mistake but it could be the problem with your file.
我知道以下是一个愚蠢的错误,但这可能是您的文件有问题。
I've renamed the file manually from adfa123
to abc.csv
. The extension of the file was hidden, after renaming, Actual File name became abc.csv.csv
. I've then removed the extra .csv
from the name and everything was fine.
我已手动将文件从 重命名adfa123
为abc.csv
. 文件的扩展名被隐藏,重命名后,实际文件名变成了abc.csv.csv
. 然后我.csv
从名称中删除了额外的内容,一切都很好。
Hope it could help anyone else.
希望它可以帮助其他人。
回答by Arjun Sanchala
import pandas as pd
path1 = 'C:\Users\Dell\Desktop\Data\Train_SU63ISt.csv'
path2 = 'C:\Users\Dell\Desktop\Data\Test_0qrQsBZ.csv'
df1 = pd.read_csv(path1)
df2 = pd.read_csv(path2)
print(df1)
print(df2)
回答by vj sreenivasan
If you are using windows machine. Try checking the file extension. There is a high possibility of file being saved as fileName.csv.txt instead of fileName.csv You can check this by selecting File name extension checkbox under folder options (Please find screenshot)
如果您使用的是 windows 机器。尝试检查文件扩展名。文件很有可能被保存为 fileName.csv.txt 而不是 fileName.csv 您可以通过选择文件夹选项下的文件扩展名复选框来检查这一点(请找到屏幕截图)
below code worked for me:
下面的代码对我有用:
import pandas as pd
df = pd.read_csv(r"C:\Users\vj_sr\Desktop\VJS\PyLearn\DataFiles\weather_data.csv");
If fileName.csv.txt, rename/correct it to fileName.csv
如果fileName.csv.txt,将其重命名/更正为fileName.csv
Hope it works, Good Luck
希望它有效,祝你好运
回答by Radmar
On Windows systems you should try with os.path.normcase
.
在 Windows 系统上,您应该尝试使用 os.path.normcase
.
It normalize the case of a pathname. On Unix and Mac OS X, this returns the path unchanged; on case-insensitive filesystems, it converts the path to lowercase. On Windows, it also converts forward slashes to backward slashes. Raise a TypeError if the type of path is not str or bytes (directly or indirectly through the os.PathLike interface).
它规范了路径名的大小写。在 Unix 和 Mac OS X 上,这将返回未更改的路径;在不区分大小写的文件系统上,它将路径转换为小写。在 Windows 上,它还会将正斜杠转换为反斜杠。如果路径类型不是 str 或 bytes (直接或间接通过 os.PathLike 接口),则引发 TypeError。
import os
import pandas as pd
script_dir = os.getcwd()
file = 'example_file.csv'
data = pd.read_csv(os.path.normcase(os.path.join(script_dir, file)))
回答by Patrick Mutuku
I had the same problem when running the file with the interactive functionality provided by Visual studio. Switched to running on the native command line and it worked for me.
使用 Visual Studio 提供的交互式功能运行文件时,我遇到了同样的问题。切换到在本机命令行上运行,它对我有用。
回答by Rakesh
I was trying to read the csv
file from the folder that was in my 'c:\'drive but, it raises the error of escape,type error, unicode
......as such but this code works
just take an variable then add r to read it.
我试图csv
从我的 'c:\' 驱动器中的文件夹中读取文件,但是,它引发了escape,type error, unicode
......因此的错误,但是这段代码只需要一个变量,然后添加 r 来读取它。
rank = pd.read_csv (r'C:\Users\DELL\Desktop\datasets\iris.csv')
df=pd.DataFrame(rank)
回答by Dee Lee
There is an another problem on how to delete the characters that seem invisible.
关于如何删除看起来不可见的字符还有一个问题。
My solution is copying the filepath from the file windows instead of the property windows.
我的解决方案是从文件窗口而不是属性窗口复制文件路径。
That is no problem except that you should fulfill the filepath.
除了您应该完成文件路径之外,这没有问题。