Python 为什么 Jupyter 无法读取 .csv 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47194211/
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
Why Jupyter can't read the .csv file?
提问by Numerical Person
I'm using python 3 in jupyter notebook. The problem is that it can't read the csv file. The command to read the csv file i have used is:
我在 jupyter notebook 中使用 python 3。问题是它无法读取 csv 文件。读取我使用的 csv 文件的命令是:
import pandas as pd
df = pd.read_csv("py.csv")
df
I have tried a lot of other ways too, but every time it says that , file is not found although the csv file in my pc.
我也尝试了很多其他方法,但每次都这样说,虽然我的电脑中有 csv 文件,但找不到文件。
FileNotFoundError Traceback (most recent call last)
<ipython-input-4-cc06427474dd> in <module>()
1 import pandas as pd
2
----> 3 df = pd.read_csv("py.csv")
4 df
~/anaconda/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)
653 skip_blank_lines=skip_blank_lines)
654
--> 655 return _read(filepath_or_buffer, kwds)
656
657 parser_f.__name__ = name
~/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
403
404 # Create the parser.
--> 405 parser = TextFileReader(filepath_or_buffer, **kwds)
406
407 if chunksize or iterator:
~/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
762 self.options['has_index_names'] = kwds['has_index_names']
763
--> 764 self._make_engine(self.engine)
765
766 def close(self):
~/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
983 def _make_engine(self, engine='c'):
984 if engine == 'c':
--> 985 self._engine = CParserWrapper(self.f, **self.options)
986 else:
987 if engine == 'python':
~/anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
1603 kwds['allow_leading_cols'] = self.index_col is not False
1604
-> 1605 self._reader = parsers.TextReader(src, **kwds)
1606
1607 # XXX
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__ (pandas/_libs/parsers.c:4209)()
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source (pandas/_libs/parsers.c:8873)()
FileNotFoundError: File b'py.csv' does not exist
回答by Saumya Pandey
Use something like this
使用这样的东西
pd.read_csv('C:\Users\user\Desktop\Workbook1.csv')
Currently you are not mentioning the path to the file so it is looking in the current working directory where the file is not present.
目前您没有提到文件的路径,因此它正在查找文件不存在的当前工作目录。
Either put the whole path or put your file in the working directory Hope it helps!
要么把整个路径放在工作目录中,要么把你的文件放在工作目录中希望它有帮助!
回答by pratha1995
You are using the right command. But specify the path for the file. If you are using Jupyter notebook than add the file py.csv to folder where your jupyter code is placed. Then it should identify the file and no longer should the error exist. Hope this helps you.
您正在使用正确的命令。但要指定文件的路径。如果您使用的是 Jupyter notebook,则将文件 py.csv 添加到放置 jupyter 代码的文件夹中。然后它应该识别文件并且不再应该存在错误。希望这对你有帮助。
回答by ak3191
Make sure you file is in the working directory and then try the below code
确保您的文件在工作目录中,然后尝试以下代码
import pandas first :
首先导入熊猫:
import pandas as pd
Read the csv file :
读取 csv 文件:
mydata = pd.read_csv("mydata.csv")
my data will be your data frame name and my data.csv
is the file name.
我的数据将是您的数据框名称,而我的数据将data.csv
是文件名。
回答by Surender Singh
Try to import os and then you will be able to see the current working directory like:
尝试导入 os ,然后您将能够看到当前的工作目录,例如:
import os
os.getcwd()
# will provide the current working directory
os.getcwd()
# 将提供当前工作目录
Now you can change the working directory to your current folder by specifying the path
现在您可以通过指定路径将工作目录更改为当前文件夹
os.chdir('C:\Users\user\Desktop')
then you can simply tread the file from the current working directory
然后你可以简单地从当前工作目录中提取文件
df = pd.read_csv("py.csv")
回答by sims_gfl
I've found out that if your .csv file is using a different delimiter/separator other than a comma, you need to specify a separator parameter like the following
我发现如果您的 .csv 文件使用逗号以外的其他分隔符/分隔符,则您需要指定一个分隔符参数,如下所示
pd.read_csv('py.csv', sep='|')
回答by md.shakhaoat hossain
you must add full path after import pandas.
您必须在导入熊猫后添加完整路径。
import pandas as pd
df = pd.read_csv('d:/Attendance-Import.csv')
df