我必须阅读名称中恰好带有句点的文件列表:
['0001.HK.csv',
'0002.HK.csv',
'0003.HK.csv',
'0004.HK.csv',
'0005.HK.csv',
'0006.HK.csv',...]
当我尝试使用熊猫读取文件时,出现错误消息:
df = pd.read_csv('001.HK.csv', index_col = 0)
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-232-a710be5c9b60> in <module>
----> 1 df = pd.read_csv('001.HK.csv', index_col = 0)
~\Anaconda3\envs\tf2\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\envs\tf2\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\envs\tf2\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\envs\tf2\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\envs\tf2\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 001.HK.csv does not exist: '001.HK.csv'
在熊猫中读取这些文件的最有效方法是什么?
答案 0 :(得分:1)
在列表中,文件具有4位数字,但是您尝试打开的文件只有3位数字。 检查文件名是否包含3或4位数字,然后打开正确的数字。
从该错误看来,当前工作目录中不存在文件001.HK.csv。 检查运行脚本的目录以及该文件是否存在。