我试图用Python读取二进制文件。其他人已使用以下代码使用R读入数据:
x <- readBin(webpage, numeric(), n=6e8, size = 4, endian = "little")
myPoints <- data.frame("tmax" = x[1:(length(x)/4)],
"nmax" = x[(length(x)/4 + 1):(2*(length(x)/4))],
"tmin" = x[(2*length(x)/4 + 1):(3*(length(x)/4))],
"nmin" = x[(3*length(x)/4 + 1):(length(x))])
对于Python,我尝试以下代码:
^{pr2}$我得到的结果略有不同。例如,R中的第一行返回4列-999.9,0,-999.0,0。Python为所有四列返回-999.0(下图)。在
我知道他们是用一些[]
代码按文件的长度进行切片,但我不知道在Python中究竟如何做到这一点,也不清楚他们为什么要这样做。基本上,我想重新创建R在Python中所做的事情。在
如果需要,我可以提供更多的代码库。我不想用不必要的代码来压倒一切。在
这里有一个不太需要内存的方法来做同样的事情。可能也快一点。(但这对我来说很难确认)
我的计算机没有足够的内存来运行第一个包含这些大文件的程序。这个是这样,但是我仍然需要先创建一个ony tmax的列表(文件的前1/4),然后打印它,然后删除这个列表,以便有足够的内存来存储nmax、tmin和nmin
但这篇文章也说,2018年文件中的nmin都是-999.0。如果这不合理,你能检查一下R代码是怎么解释的吗?我怀疑这只是档案里的东西。另一种可能性当然是,我完全搞错了(我对此表示怀疑)。不过,我也尝试了2017年的文件,但这个文件没有这样的问题:所有tmax、nmax、tmin、nmin都有大约37%-999.0的值
不管怎样,下面是第二个代码:
从R代码推断,二进制文件首先包含一个特定的数}和{}。该代码所做的是读取整个文件,然后使用切片将其分成4个部分(tmax、nmax等)。在
tmax
,然后是相同数量的nmax
,然后是{要在python中执行相同操作,请执行以下操作:
如果目标是将此数据结构化为点列表(?)像
^{pr2}$(tmax,nmax,tmin,nmin)
,然后将其附加到代码中:相关问题 更多 >
编程相关推荐