【问题标题】:FileNotFoundError: [Errno 2] File C:FileNotFoundError:[Errno 2] 文件 C:
【发布时间】:2020-09-15 04:24:48
【问题描述】:

所以我试图通过 pandas 导入一个带有 python 的 csv 文件。代码是

import pandas as pd

df = pd.read_csv(r"C:\Users\B1880\Downloads\avocado-prices.zip\avocado.csv")

虽然我不断收到错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-106-03cc5a9f89de> in <module>
      1 import pandas as pd
      2 
----> 3 df = pd.read_csv(r"C:\Users\B1880\Downloads\avocado-prices.zip\avocado.csv")
      4 d

~\Anaconda3\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, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    674         )
    675 
--> 676         return _read(filepath_or_buffer, kwds)
    677 
    678     parser_f.__name__ = name

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    446 
    447     # Create the parser.
--> 448     parser = TextFileReader(fp_or_buf, **kwds)
    449 
    450     if chunksize or iterator:

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
    878             self.options["has_index_names"] = kwds["has_index_names"]
    879 
--> 880         self._make_engine(self.engine)
    881 
    882     def close(self):

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
   1112     def _make_engine(self, engine="c"):
   1113         if engine == "c":
-> 1114             self._engine = CParserWrapper(self.f, **self.options)
   1115         else:
   1116             if engine == "python":

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, src, **kwds)
   1889         kwds["usecols"] = self.usecols
   1890 
-> 1891         self._reader = parsers.TextReader(src, **kwds)
   1892         self.unnamed_cols = self._reader.unnamed_cols
   1893 

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

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

FileNotFoundError: [Errno 2] File C:\Users\B1880\Downloads\avocado-prices.zip\avocado.csv does not exist: 'C:\\Users\\B1880\\Downloads\\avocado-prices.zip\\avocado.csv'

当我知道我有正确的整个文件路径时,我不明白它是如何找不到文件的。我去了窗口右下角的搜索栏,然后我查找了文件并复制了完整路径。我已经尝试使用文件前面的 r 而没有它。我尝试了双反斜杠和单反斜杠。如果这是它的路径,它怎么找不到文件?

【问题讨论】:

  • 看起来您正试图访问 *.zip 文件中的文件。您可能想先尝试解压缩文件。或者,直接尝试读取 zip 文件。
  • 如果文件名相同,我相信 pandas 应该能够读取压缩的 .csv 文件。例如avocado.csv -> avocado.zip
  • @monkit 感谢您的回复。我将如何做这两件事?
  • 非常感谢!!!!你是对的,我所要做的就是解压缩文件!谢谢谢谢。 (对不起,我已经在这几个小时了:))

标签: python pandas csv file jupyter-notebook


【解决方案1】:

尝试使用这里提到的zipfile 模块 -> Reading csv zipped files in python

这样的事情应该可以工作:

import pandas as pd
import zipfile

zf = zipfile.ZipFile('C:\Users\B1880\Downloads\avocado-prices.zip') 
df = pd.read_csv(zf.open('avocado.csv'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    相关资源
    最近更新 更多