首页 > 解决方案 > pandas.errors.EmptyDataError,但文件不为空

问题描述

我编写了以下代码(缩短)来从 netCDF 文件中检索数据,并将它们作为时间序列保存在每个循环中每个 i,j 单元格的临时文件中:

for i in range(0,400):
    for j in range(0,120):
       with open('file_temp.tmp', 'w') as out_temp:
           out_temp.write('header1'+'\t'+'header2'+'\n')
           for yr in range(1990,2011):
               (get data from netCDF)
               out_temp.write(str(val1)+'\t'+str(val2)+'\n')
       df=pd.read_csv('file_temp.tmp', delimiter='\t')

但是在 Pandas 读取文件的最后一行中,我收到了这个错误:

 line 1605, in __init__
self._reader = parsers.TextReader(src, **kwds)
  File "pandas/_libs/parsers.pyx", line 565, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file

我会很感激你的帮助

标签: pythonpandaserror-handling

解决方案


对谁来说,这个问题可能是一个问题,我找到了一种解决方案,这可能看起来有点幼稚,但有效并且至少在紧急情况下可以被视为捷径:

我将我的脚本分成两个脚本,一个用于提取数据并以时间序列的形式将它们写入 csv 文件,另一个脚本用于读取这些 csv 文件并进一步处理它们。就像是:

第一个脚本:

for i in range(0,400):
    for j in range(0,120):
       with open('file_temp.tmp', 'w') as out_temp:
       out_temp.write('header1'+'\t'+'header2'+'\n')
           for yr in range(1990,2011):
               (get data from netCDF)
               out_temp.write(str(val1)+\
               '\t'+str(val2)+'\n')

第二个脚本:

df=pd.read_csv('file_temp.tmp', delimiter='\t')
(do further analysis)

如果您使用的是 Unix 或 Linux,则 bash 脚本可以同时运行这两个脚本(连续),您不必一个接一个地运行


推荐阅读