[英]Reading csv files in Pandas
I must read a list of files that happen to have in their names a fullpoint:我必须阅读一个文件列表,这些文件碰巧在他们的名字中有一个完整的点:
['0001.HK.csv',
'0002.HK.csv',
'0003.HK.csv',
'0004.HK.csv',
'0005.HK.csv',
'0006.HK.csv',...]
When I try to read the file with pandas I get an error message:当我尝试使用 Pandas 读取文件时,我收到一条错误消息:
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'
What would be the most efficient way to read these files in pandas?在熊猫中读取这些文件的最有效方法是什么?
In the list the files have 4 digits number but you are trying to open a file with only 3 digits number.在列表中,文件有 4 位数字,但您试图打开只有 3 位数字的文件。 Check if the name of the files have 3 or 4 digits and open the correct one.检查文件名是否有 3 位或 4 位数字,然后打开正确的。
From the error it looks like the file 001.HK.csv doesn't exist in the current working directory.从错误看来,当前工作目录中不存在文件 001.HK.csv。 Check the directory where you are running the script and if that file exists there.检查您运行脚本的目录以及该文件是否存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.