0

I have a 3D-array storing temperature data. And me want to put it to text file in one single line. Need to re-write this code in pythonic way.

    for jn in range(X1, X2):
        for jm in range(Y1,Y2):
            fl.write(str((t[jn,jm] - 273.1).astype(int))+" ")
    fl.write("\n")
Serenity
  • 35,289
  • 20
  • 120
  • 115

2 Answers2

4

Assuming your array to save is t:

t.tofile('yourfile.txt',sep=" ",format="%s")

Also see this question:

How to write a multidimensional array to a text file?

Community
  • 1
  • 1
Lee
  • 29,398
  • 28
  • 117
  • 170
2
values = ("{0:.0f}".format(t[i,j]-273.1) 
                for i in xrange(X1, X2) 
                for j in xrange(Y1,Y2))
line = " ".join(values)
fl.write(line + "\n")
Thomas B.
  • 2,276
  • 15
  • 24