python - 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
我会很感激你的帮助
解决方案
对谁来说,这个问题可能是一个问题,我找到了一种解决方案,这可能看起来有点幼稚,但有效并且至少在紧急情况下可以被视为捷径:
我将我的脚本分成两个脚本,一个用于提取数据并以时间序列的形式将它们写入 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 脚本可以同时运行这两个脚本(连续),您不必一个接一个地运行
推荐阅读
- android - 如何将 java 和 xml 代码(从 api 获取)运行到另一个片段中
- javascript - 鼠标悬停时滑出 div 标签
- javascript - 如何在 asp.net 核心的托管网站上运行脚本?
- javascript - 淡入黑色过渡
- c# - xamarin 形式的可扩展树视图
- javascript - 我想使用 javascript 动态创建标签
- reactjs - 动作的调度是否在反应流的后台运行?
- excel - 使用excel公式为超过2个空格的文本创建缩写
- elasticsearch - elasticsearch排序搜索返回中的_doc是什么意思?
- javascript - 如何在 Vue.js 中将带有一些数组对象值的 Post 请求发送到我的后端