pandas 即使文件存在,文件 b'train.csv' 也不存在

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

File b'train.csv' does not exist even though file exist

pythonpython-3.xpandascsvtensorflow

提问by Yun Jie Lim

Code:

代码:

import pandas as pd
train_df = pd.read_csv("train.csv")

Error:

错误:

FileNotFoundError                   Traceback (most recent call last)
    <ipython-input-17-05c7c432b69f> in <module>()
      1 import pandas as pd
      2 
    ----> 3 train_df = pd.read_csv("../input/train.csv")

    /anaconda3/lib/python3.6/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)
    707                     skip_blank_lines=skip_blank_lines)
    708 
    --> 709         return _read(filepath_or_buffer, kwds)
    710 
    711     parser_f.__name__ = name

    /anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    447 
    448     # Create the parser.
    --> 449     parser = TextFileReader(filepath_or_buffer, **kwds)
    450 
    451     if chunksize or iterator:

    /anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
    816             self.options['has_index_names'] = kwds['has_index_names']
    817 
    --> 818         self._make_engine(self.engine)
    819 
    820     def close(self):

    /anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
    1047     def _make_engine(self, engine='c'):
    1048         if engine == 'c':
    -> 1049             self._engine = CParserWrapper(self.f, **self.options)
    1050         else:
    1051             if engine == 'python':

    /anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
    1693         kwds['allow_leading_cols'] = self.index_col is not False
    1694 
    -> 1695         self._reader = parsers.TextReader(src, **kwds)
    1696 
    1697         # XXX

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

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

FileNotFoundError: File b'../input/train.csv' does not exist

Please help! I tried using pd.read_csv("../input/train.csv")but there is still an error. I am a Mac user using Jupyter notebook.

请帮忙!我尝试使用pd.read_csv("../input/train.csv")但仍然有错误。我是使用 Jupyter 笔记本的 Mac 用户。

采纳答案by Julia

  1. Are you sure you have the correct path?
  1. 你确定你有正确的路径吗?

train_df = pd.read_csv("./input/train.csv") (if the csv file is in the input folder which is in the same folder as your jupyter notebook)

train_df = pd.read_csv("./input/train.csv") (如果 csv 文件位于输入文件夹中,该文件夹与您的 jupyter 笔记本位于同一文件夹中)

Easiest would be you have a folder which contains the juptyer notebook and the csv file. Then you would just need to do:

最简单的方法是您有一个包含 juptyer 笔记本和 csv 文件的文件夹。然后你只需要做:

train_df = pd.read_csv("./train.csv") or train_df = pd.read_csv("train.csv")

train_df = pd.read_csv("./train.csv") 或者 train_df = pd.read_csv("train.csv")

  1. Try using train_df = pd.read_csv("train.csv",encoding='utf-8' )
  1. 尝试使用 train_df = pd.read_csv("train.csv",encoding='utf-8' )

to get rid of the 'b in front of b'../input/train.csv'

摆脱 'b 前面的 b'../input/train.csv'

回答by Sandeep Amarnath

Try using absolute path like this. The r at the beginning of the line helps to read the whole string as a raw string as is, so when r is used you don't have to worry about escaping slashes

尝试使用这样的绝对路径。该行开头的 r 有助于将整个字符串按原样读取为原始字符串,因此当使用 r 时,您不必担心转义斜杠

import pandas

myFile = pandas.read_csv(r"C:\Users\samarnat\Documents\Personal Docs\Projects\train.csv",encoding='utf-8')

回答by robin9029

I found, if you are using new version of Excel extension than you will face the problem. first of all save the file eg.

我发现,如果您使用的是新版本的 Excel 扩展程序,您将面临这个问题。首先保存文件,例如。

1) save as fileName = test

1)另存为文件名=测试

2) below name section select - CSV UTF-8 (Comma Delimited )(*.csv)

2) 在名称部分下方选择 - CSV UTF-8 (Comma Delimited )(*.csv)

now use Jupyter

现在使用 Jupyter

import pandas as pd test = pd.read_csv("test.csv")

导入Pandas为 pd test = pd.read_csv("test.csv")

回答by Nisarg Bhatt

Possible Reasons:

可能的原因:

  1. Path entered is incorrect or multiple folders of same name.
  2. The name of the file is incorrect.
  3. The file extension is not csv, it maybe xlsx(Rare chance)
  1. 输入的路径不正确或有多个同名文件夹。
  2. 文件名不正确。
  3. 文件扩展名不是csv,它可能xlsx(罕见的机会)

Hope this helps.

希望这可以帮助。

回答by Shardul Singh

Try to give an absolute path of the file rather than giving a relative one.

尝试给出文件的绝对路径而不是相对路径。

回答by Gauda

Check for empty spacesspace characters at the beginning or end of your file string.

检查文件字符串开头或结尾的空格字符。

回答by Avyush Pandey

if your text or csv file is in same folder where your jupyter notebook then instead of writing pd.read_csv('test.csv') write as pd.read_csv("test") bcz if your csv file explicitly shows .csv extension then only first way work or else second way. for example if your file name looks like "test" then use pd.read_csv('test') or else filename is "test.csv" then use pd.read_csv('test.csv') It depend on saving name of file.

如果您的文本或 csv 文件位于您的 jupyter 笔记本所在的同一文件夹中,则不要编写 pd.read_csv('test.csv') 而是写为 pd.read_csv("test") bcz 如果您的 csv 文件明确显示 .csv 扩展名,则仅第一种方式工作,否则第二种方式。例如,如果您的文件名看起来像“test”然后使用 pd.read_csv('test') 或者文件名是“test.csv”然后使用 pd.read_csv('test.csv') 这取决于保存文件的名称。