简体   繁体   English

在 Pandas 中读取 csv 文件

[英]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.

相关问题 有效地将多个csv文件读取到Pandas数据框中 - Efficiently reading multiple csv files into Pandas dataframe 当其中有HTML转义字符串时,使用python(pandas)读取CSV文件 - Reading CSV files with python (pandas) when there is HTML escaped string in there 用熊猫读取CSV并忽略逗号 - Reading CSV with pandas and ignoring commas 使用 Pandas 编辑 CSV 文件 - Editing CSV files with Pandas 使用指定了dtypes的pandas读取大型csv文件会导致内存错误? - Reading large csv files using pandas with specifying dtypes giving memory error? 将多个 csv 文件读取到 HDF5 时 Pandas ParserError EOF 字符 - Pandas ParserError EOF character when reading multiple csv files to HDF5 在 pandas 数据框中读取和写入 csv 文件时防止列表列表中出现双引号 - Prevent double quotes in lists of list when reading and writing csv files in pandas dataframe 用于从Google存储桶将多个csv文件读取到1个Pandas DataFrame中的“ For”循环 - 'For' loop for reading multiple csv files from a google storage bucket into 1 Pandas DataFrame 从RAM读取CSV文件 - Reading CSV files from RAM 将所有.csv文件从Google存储桶中读取到一个大熊猫df中,然后另存为.csv到另一个存储桶中 - Reading all .csv files from a google storage bucket into one large pandas df, then saving back as .csv to another bucket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM