python - 在 Jupyter notebook 中用 pandas 读取 csv
问题描述
这是我用来将 csv 文件读取到 Jupyter 笔记本的代码。
import pandas as pd
data = pd.read_csv("C:\\Users\\Mike\\Desktop\\File.csv")
for row in data:
print(row)
if len(data) > 5:
break
我也试过
data = pd.read_csv(r"C:\Users\Mike\Desktop\File.csv")
导致以下错误
FileNotFoundError Traceback (most recent call last)
<ipython-input-7-332200c5442e> in <module>
1 import pandas as pd
2
----> 3 data = pd.read_csv("C:\\Users\\Mike\\Desktop\\File.csv")
4 for row in data:
5 print(row)
/srv/conda/envs/notebook/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, 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
/srv/conda/envs/notebook/lib/python3.6/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:
/srv/conda/envs/notebook/lib/python3.6/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):
/srv/conda/envs/notebook/lib/python3.6/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":
/srv/conda/envs/notebook/lib/python3.6/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\Mike\Desktop\File.csv does not exist: 'C:\\Users\\Mike\\Desktop\\File.csv'
尝试后
导入操作系统
if os.path.isfile('C:\Users\Mike\Desktop\File.csv'): print('文件存在') else: print('文件不存在')
正如@LittinRajan 所建议的,我知道该文件不存在。好吧,显然该文件确实存在,而且我使用的是正确的路径,所以它可能是 Jupyter 的东西。任何帮助将不胜感激,因为我是 Python 和 Jupyter 的新手
解决方案
推荐阅读
- java - 如何更改“\t”的空格?
- javascript - 如何用动画重新渲染 React 组件
- node.js - 如何在 REST API 中公开 MongoDB 文档主键?
- java - Java looped Scanner.hasNext() 在第一次评估为假后总是评估为假
- javascript - React - 使用多个数组映射
- emacs - 在键盘映射命令之前运行函数
- python - 如何阻止 Jupyter 打印出修改后的绘图参数?
- sql-server - 使用链接服务器调用 sp_cursoropen 会引发错误
- python - 使用正则表达式在标记后获取第二个 div
- ios - Xcode 11 中的构建/归档无限期挂起