pandas 无法读取/打开/或对 CSV 文件执行任何操作 python 3.4 windows
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23572520/
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
Can't read/open/ or do anything with a CSV file python 3.4 windows
提问by Kuzen
I am unable to open any CSV file produced by a program(I don't have full details of program), it comes out with file names like 266925.130314-88850999.word
我无法打开程序生成的任何 CSV 文件(我没有程序的完整详细信息),它的文件名类似于 266925.130314-88850999.word
The file is a csv, it opens in excel 2013 and all editors fine, I have tried opening with original file name, I have tried opening by changing extension from .word to .csv
该文件是一个 csv,它在 excel 2013 中打开,所有编辑器都很好,我尝试使用原始文件名打开,我尝试通过将扩展名从 .word 更改为 .csv 来打开
Nothing works, I get the errors detailed below.
没有任何效果,我得到了下面详细的错误。
Sample Part of CSV
CSV 的示例部分
"tes","","2PT26","",4,7,"TEST","SHEEP AND STUFFF 1kg","",1.1111,9.9999,1,0.000,2.6900,0.0000,0.000,
Full error Message in idle gui
空闲 gui 中的完整错误消息
>>> open('C:\Console2\edz\test.csv', 'r', newline='', encoding='utf-8')
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
open('C:\Console2\edz\test.csv', 'r', newline='', encoding='utf-8')
OSError: [Errno 22] Invalid argument: 'C:\Console2\edz\test.csv'
Pandas error (not given full because its massive amount of text with traceback - can add if required)
Pandas 错误(没有给出完整的,因为它有大量带有回溯的文本 - 如果需要可以添加)
OSError: File b'C:\Console2\edz\test.csv' does not exist
I have tested this file exists with
我已经测试过这个文件存在
files = os.listdir("C:\Console2\edz")
print (files)
['report.csv', 'test.csv']
I can use EXACT same code with report.csv (this csv is a known working one not created by program) and it loads fine.
我可以在 report.csv 中使用完全相同的代码(这个 csv 是一个已知的工作,不是由程序创建的)并且它加载得很好。
I do not understand why this file wont open, I have tried open/read_csv/read_excel, all produce same errors on both idle and pandas.
我不明白为什么这个文件打不开,我试过 open/read_csv/read_excel,都在空闲和Pandas上产生相同的错误。
Pulling my hair out, please assist.
拉我的头发,请帮忙。
回答by Gareth Latty
The issue here is that Python uses escapesin strings - '\t'is a tab character, so when that sequence appears in your path, it changes meaning (note how in the output, the other backslashes have been escaped). Python is looking for 'C:\Console2\edz est.csv'.
这里的问题是 Python在字符串中使用转义'\t'符-是制表符,因此当该序列出现在您的路径中时,它的含义会改变(注意在输出中,其他反斜杠是如何被转义的)。Python 正在寻找'C:\Console2\edz est.csv'.
The best solution is to use forward slashes for paths - Python will do the correct thing, even under Windows.
最好的解决方案是对路径使用正斜杠 - 即使在 Windows 下,Python 也会做正确的事情。
open('C:/Console2/edz/test.csv', 'r', newline='', encoding='utf-8')
You can also use a raw string (r'C:\Console2\edz\test.csv') or escape your backslashes ('C:\\Console2\\edz\\test.csv') - but the former can't deal with trailing backslashes, and the latter is hard to read and write.
您还可以使用原始字符串 ( r'C:\Console2\edz\test.csv') 或转义反斜杠 ( 'C:\\Console2\\edz\\test.csv') - 但前者无法处理尾随反斜杠,而后者难以读写。
回答by Padraic Cunningham
\tis an escape character in python, try \\t.
\t是python中的转义字符,试试 \\t.
C:\Console2\edz\test.csv
you can also prefix the string with r'raw string' before the path (r'C:\Console2\edz\test.csv'','r')
您还可以r在路径前使用“原始字符串” 作为字符串前缀 (r'C:\Console2\edz\test.csv'','r')

