I have a 2D numpy array. I want to make a ASCII file out of it. Here's the code I am using where a3
is the numpy array
我有一个2D numpy数组。我想用它制作一个ASCII文件。这是我使用的代码,其中a3是numpy数组
f = open('ASCIIout.asc', 'w')
numpy.savetxt(ASCIIout, a3)
f.write("ncols " + str(ncols) + "\n")
f.write("nrows " + str(nrows) + "\n")
f.write("xllcorner " + str(xllcorner) + "\n")
f.write("yllcorner " + str(yllcorner) + "\n")
f.write("cellsize " + str(cellsize) + "\n")
f.write("NODATA_value " + str(noDATA) + "\n")
f.close()
Now I open it with append option and write the 2D array values:
现在我用append选项打开它并写入2D数组值:
f_handle = open(ASCIIout, 'a')
numpy.savetxt(f_handle, MyArray, fmt="%.3f")
f_handle.close()
However, I have a couple problems. First, the fmt
really does not work I get values like this:
但是,我有几个问题。首先,fmt真的不起作用我得到这样的值:
9.999000000000000000e+03
If I JUST use the code below line of code, I get -9999.000 1.345
, but then, I haven't attached ncols, nrows, etc to the ASCII file.
如果我只是使用下面的代码行代码,我得到-9999.000 1.345,但是,我还没有将ncols,nrows等附加到ASCII文件中。
numpy.savetxt(f_handle, MyArray, fmt="%.3f")
My data range from 0 to 6. What I really want to get is:
我的数据范围从0到6.我真正想要得到的是:
-9999 1.345 -9999 3.21 0.13 -9999
where I do NOT get decimals after -9999
and I do get decimals after real data such as 1.345
I made the ASCII file in R, I am wondering there should be a easy way to do it in Python. Thanks for your help in advance.
在-9999之后我没有得到小数,我在实际数据之后得到小数,例如1.345我在R中制作了ASCII文件,我想知道在Python中应该有一个简单的方法。感谢您的帮助。
1 个解决方案
#1
1
If your intention is to have the "stop value" -9999 appear without the decimal point, you can let savetxt()
save all the values with the "%.3f"
format, and replace the one result you don't want with the one you want:
如果您打算在没有小数点的情况下出现“停止值”-9999,您可以让savetxt()以“%。3f”格式保存所有值,并将您不想要的一个结果替换为你想要的一个:
from StringIO import StringIO
import numpy as np
f = StringIO()
x = np.array(( -9999, 1.345, -9999, 3.21, 0.13, -9999), dtype=float)
np.savetxt(f, x, fmt='%.3f')
f.seek(0)
fs = f.read().replace('-9999.000', '-9999', -1)
f.close()
f = open('ASCIIout.asc', 'w')
f.write("ncols " + str(ncols) + "\n")
f.write("nrows " + str(nrows) + "\n")
f.write("xllcorner " + str(xllcorner) + "\n")
f.write("yllcorner " + str(yllcorner) + "\n")
f.write("cellsize " + str(cellsize) + "\n")
f.write("NODATA_value " + str(noDATA) + "\n")
f.write(fs)
f.close()
If you want all the integer values represented without decimals, .replace('.000', '', -1)
would accomplish that.
如果你想要所有表示没有小数的整数值,.replace('。000','', - 1)就可以实现这一点。
#1
1
If your intention is to have the "stop value" -9999 appear without the decimal point, you can let savetxt()
save all the values with the "%.3f"
format, and replace the one result you don't want with the one you want:
如果您打算在没有小数点的情况下出现“停止值”-9999,您可以让savetxt()以“%。3f”格式保存所有值,并将您不想要的一个结果替换为你想要的一个:
from StringIO import StringIO
import numpy as np
f = StringIO()
x = np.array(( -9999, 1.345, -9999, 3.21, 0.13, -9999), dtype=float)
np.savetxt(f, x, fmt='%.3f')
f.seek(0)
fs = f.read().replace('-9999.000', '-9999', -1)
f.close()
f = open('ASCIIout.asc', 'w')
f.write("ncols " + str(ncols) + "\n")
f.write("nrows " + str(nrows) + "\n")
f.write("xllcorner " + str(xllcorner) + "\n")
f.write("yllcorner " + str(yllcorner) + "\n")
f.write("cellsize " + str(cellsize) + "\n")
f.write("NODATA_value " + str(noDATA) + "\n")
f.write(fs)
f.close()
If you want all the integer values represented without decimals, .replace('.000', '', -1)
would accomplish that.
如果你想要所有表示没有小数的整数值,.replace('。000','', - 1)就可以实现这一点。