[英]EmptyDataError: No columns to parse from file when loading several files in a dictionary
I have 1000 csv files that I call using the following code (which puts every file into a dictionary):我有 1000 个 csv 文件,我使用以下代码(将每个文件放入字典)调用它们:
dataframes = {}
csv_root = Path(".")
for csv_path in csv_root.glob("*.csv"):
key = csv_path.stem
dataframes[key] = pd.read_csv(csv_path, skiprows=1)
However when I use this code I got this error但是,当我使用此代码时,出现此错误
EmptyDataError: No columns to parse from file
Which indicates that there is empty data or header is encountered.这表明遇到空数据或标题。
I would like to know how to identify which of those 1000 csv files are the ones causing troubles?我想知道如何识别这 1000 个 csv 文件中的哪些是造成问题的文件? Because, as you can understand, checking file by file will consume a lot of time.
因为,正如您所理解的,逐个文件检查会消耗大量时间。
Thanks a lot!非常感谢!
I would just use a try/catch, like so:我只会使用 try/catch,如下所示:
dataframes = {}
csv_root = Path(".")
for csv_path in csv_root.glob("*.csv"):
key = csv_path.stem
try:
dataframes[key] = pd.read_csv(csv_path, skiprows=1)
except Exception, as e:
dataframes[key] = 'error' # mark the errored
This last step will get you the stems with issues:最后一步将使您了解问题的根源:
errored_stems = [k for k,v in dataframes.iteritems() if k == 'error']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.