Python OSError:在 Pandas 的 csv 上从文件初始化失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50552404/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:31:50  来源:igfitidea点击:

OSError: Initializing from file failed on csv in Pandas

pythonpandascsvdataframe

提问by rStorms

So far pandasread through all my CSV files without any problem, however now there seems to be a problem..

到目前为止,pandas通读了我所有的 CSV 文件没有任何问题,但是现在似乎有问题..

When doing:

做的时候:

df = pd.read_csv(r'path to file', sep=';')

I get:

我得到:

OSError Traceback (most recent call last) in () ----> 1 df = pd.read_csv(r'path übersicht\Input\test\test.csv', sep=';')

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision) 703 skip_blank_lines=skip_blank_lines) 704 --> 705 return _read(filepath_or_buffer, kwds) 706 707 parser_f.name= name

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds) 443 444 # Create the parser. --> 445 parser = TextFileReader(filepath_or_buffer, **kwds) 446 447 if chunksize or iterator:

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in init(self, f, engine, **kwds) 812 self.options['has_index_names'] = kwds['has_index_names'] 813 --> 814 self._make_engine(self.engine) 815 816 def close(self):

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine) 1043 def _make_engine(self, engine='c'): 1044 if engine == 'c': -> 1045 self._engine = CParserWrapper(self.f, **self.options) 1046 else: 1047 if engine == 'python':

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in init(self, src, **kwds) 1682 kwds['allow_leading_cols'] = self.index_col is not False 1683 -> 1684 self._reader = parsers.TextReader(src, **kwds) 1685 1686 # XXX

pandas_libs\parsers.pyx in pandas._libs.parsers.TextReader.cinit()

pandas_libs\parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

OSError: Initializing from file failed

OSError Traceback(最近一次调用最后一次) in () ----> 1 df = pd.read_csv(r'path übersicht\Input\test\test.csv', sep=';')

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in parser_f(filepath_or_buffer、sep、delimiter、header、names、index_col、usecols、squeeze、prefix、mangle_dupe_cols、dtype、engine、converters, true_values、false_values、skipinitialspace、skiprows、nrows、na_values、keep_default_na、na_filter、verbose、skip_blank_lines、parse_dates、infer_datetime_format、keep_date_col、date_parser、dayfirst、迭代器、chunksize、压缩、千、十进制、行注释符、escapechar、编码、方言、tupleize_cols、error_bad_lines、warn_bad_lines、skipfooter、skip_footer、双引号、delim_whitespace、as_recarray、compact_ints、use_unsigned、low_memory、buffer_lines、memory_map、float_precision) 703 skip_blank_lines=skip_blank4_lines>7705 返回 _read(filepath_or_buffer, kwds) 706 707 parser_f。姓名= 姓名

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds) 443 444 # 创建解析器。--> 445 parser = TextFileReader(filepath_or_buffer, **kwds) 446 447 如果块大小或迭代器:

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in init(self, f, engine, **kwds) 812 self.options['has_index_names'] = kwds['has_index_names'] 813 --> 814 self._make_engine(self.engine) 815 816 def close(self):

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine) 1043 def _make_engine(self, engine='c'): 1044 if engine == 'c': -> 1045 self._engine = CParserWrapper(self.f, **self.options) 1046 else: 1047 if engine == 'python':

c:\program files\python36\lib\site-packages\pandas\io\parsers.py in init(self, src, **kwds) 1682 kwds['allow_leading_cols'] = self.index_col is not False 1683 -> 1684 self._reader = parsers.TextReader(src, **kwds) 1685 1686 # XXX

pandas._libs.parsers.TextReader 中的 pandas_libs\parsers.pyx。cinit()

pandas._libs.parsers.TextReader._setup_parser_source() 中的 pandas_libs\parsers.pyx

OSError:从文件初始化失败

Other files in the same folder that are XLS files can be accessed without an issue.

可以毫无问题地访问同一文件夹中作为 XLS 文件的其他文件。

When using the Python library like so:

像这样使用 Python 库时:

import csv
file = csv.reader(open(r'pathtofile')) 

for row in file:
    print(row)
    break

df = pd.read_csv(file, sep=';')

the file is being loaded and the first line is printed. However I get:

正在加载文件并打印第一行。但是我得到:

ValueError: Invalid file path or buffer object type:

ValueError:无效的文件路径或缓冲区对象类型:

Probably because I can't use read_csvthis way...

可能是因为我不能用read_csv这种方式...

How to get the first pandasfunction to work? The csv does not contain any special characters except German ones. The filesize is 10MB.

如何让第一个pandas函数起作用?除德语字符外,csv 不包含任何特殊字符。文件大小为 10MB。

回答by Dane Lee

import pandas as pd
pd.read_csv("your_file.txt", engine='python')

Try this. It totally worked for me.

尝试这个。它完全适合我。

回答by Breck

I ran into a similar problem. It turned out the CSV I had downloaded had no permissions at all. The error message from pandas did not point this out, making it hard to debug.

我遇到了类似的问题。原来我下载的 CSV 根本没有权限。pandas 的错误信息没有指出这一点,因此很难调试。

Check that your file have read permissions

检查您的文件是否具有读取权限

回答by Kornel Dylski

I had the same issue, you should check your permissions.

我有同样的问题,你应该检查你的权限。

After chmod 644 file.csvit worked well.

之后chmod 644 file.csv,效果不错。

回答by Sewen Scorpius

I find the same problem under Win10 OS when I try to read a csv file which name is in Chinese. Then there's no problem any more after I rename my file into EN. Maybe you should ensure your full csv file path in EN.

我在 Win10 操作系统下尝试读取中文名称的 csv 文件时发现同样的问题。然后我将文件重命名为 EN 后就没有问题了。也许您应该确保在 EN 中使用完整的 csv 文件路径。

OS:Windows 10; Python version:3.6.5; Ipython:7.0.1; Pandas:0.23.0

操作系统:Windows 10;Python版本:3.6.5;蟒蛇:7.0.1;熊猫:0.23.0

First, when use

一、使用时

import pandas as pd
answer_df = pd.read_csv('./答案.csv')

my ipython notebook raise 'OSError: Initializing from file failed'.

我的 ipython 笔记本引发“OSError:从文件初始化失败”。

Then I rename my file into answers.csv

然后我将我的文件重命名为 answers.csv

import pandas as pd
answer_df = pd.read_csv('./answers.csv')

everythings' OK.

一切都没事。

May to help you.

可以帮你。

回答by QaraQoyunlu

Same problem when I was trying to load files with Japanese filenames.

当我尝试加载带有日语文件名的文件时,同样的问题。

import pandas as pd
result = pd.read_csv('./result/けっこう.csv')

OSError: Initializing from file failed'

Then I added an argument engine="python".

然后我添加了一个论点engine="python"

result = pd.read_csv('./result/けっこう.csv', engine="python")

It worked for me.

它对我有用。

回答by kunal Vyas

Ran into the same issue on windows. Tried to use to the solution provided by Dan Lee, but was getting abnormal results with rows and columns. I am not sure whether this was because of the Japanese characters in my csv files or not but by clearly defining the encoding format solved the problem for me.

在 Windows 上遇到了同样的问题。尝试使用 Dan Lee 提供的解决方案,但行和列的结果异常。我不确定这是否是因为我的 csv 文件中的日语字符,但通过明确定义编码格式为我解决了这个问题。

import pandas as pd
pd.read_csv("your_file.txt", engine='python', encoding = "utf-8-sig")

回答by Ranee

I assume that your csv file is in the same place (root). If you just want to the csv file to be read, and get the result that will show as text in your console, just do this

我假设您的 csv 文件在同一个地方(根)。如果您只想读取 csv 文件,并获得将在控制台中显示为文本的结果,请执行以下操作

import csv
with open('your_file.csv', 'r') as csvFile:
    reader = csv.reader(csvFile)
    for row in reader:
        print(row)
csvFile.close()

note: the code is for Python 3, if you use Python 2 the print syntax use without brackets. Hope this will help you

注意:代码适用于 Python 3,如果您使用 Python 2,则使用不带括号的打印语法。希望能帮到你

回答by Varun kumar

You can try using os.path.join()to build your path:

您可以尝试使用os.path.join()来构建您的路径:

import os

rpath = os.path.join('U:','folder','Input','test.csv')
df = pd.read_csv(rpath, sep=';')

To traverse path based on your parent directory, you can use:

要根据您的父目录遍历路径,您可以使用:

os.path.pardir

回答by gkennos

Same problem different solution here.

同样的问题在这里不同的解决方案。

I had previously tried to load my file in excel, and excel had crashed but must have retained some file lock because once I force-quit excel, it loaded as expected.

我以前曾尝试在 excel 中加载我的文件,但 excel 崩溃了,但必须保留一些文件锁定,因为一旦我强制退出 excel,它就会按预期加载。

回答by icanxy

pandas read_csv OSError: Initializing from file failed

pandas read_csv OSError:从文件初始化失败

We could try chmod 600 file.csv.

我们可以试试chmod 600 file.csv