0

I am pretty new to Python and trying to kick my Matlab addiction. I am converting a lot of my lab's machine vision code over to Python but I am just stuck on one aspect of the saving. At each line of the code we save 6 variables in an array. I'd like these to be entered in as one of 6 columns in a txt file with bumpy.savetxt. Each iteration of the tracking loop would then add similar variables for that given frame as the next row in the txt file.

But I keep getting either a single column that just grows with every loop. I've attached a simple code to show my problem. As it loops through, there will be a variable generated that is called output. I would like this to be the three columns of the txt file and each iteration of the loop to be a new row. Is there any easy way to do this?

import numpy as np

dataFile_Path = "dataFile.txt"
dataFile_id = open(dataFile_Path, 'w+')

for x in range(0, 9):
    variable = np.array([2,3,4])
    output = x*variable+1
    output.astype(float)
    print(output)
    np.savetxt(dataFile_id, output, fmt="%d")

dataFile_id.close()

1 Answer 1

1
In [160]: for x in range(0, 9):
     ...:     variable = np.array([2,3,4])
     ...:     output = x*variable+1
     ...:     output.astype(float)
     ...:     print(output)
     ...:     
[1 1 1]
[3 4 5]
[5 7 9]
[ 7 10 13]
[ 9 13 17]
[11 16 21]
[13 19 25]
[15 22 29]
[17 25 33]

So you are writing one row at a time. savetxt normally is used to write a 2d array.

Notice that the print is still integers - astype returns a new array, it does not change things inplace.

But because you are giving it 1d arrays it writes those as columns:

In [177]: f = open('txt','bw+')
In [178]: for x in range(0, 9):
     ...:     variable = np.array([2,3,4])
     ...:     output = x*variable+1
     ...:     np.savetxt(f, output, fmt='%d')
     ...:     
In [179]: f.close()
In [180]: cat txt
1
1
1
3
4
5
5
7
9

if instead I give savetxt a 2d array ((1,3) shape), it writes

In [181]: f = open('txt','bw+')
In [182]: for x in range(0, 9):
     ...:     variable = np.array([2,3,4])
     ...:     output = x*variable+1
     ...:     np.savetxt(f, [output], fmt='%d')
     ...:     
     ...:     
In [183]: f.close()
In [184]: cat txt
1 1 1
3 4 5
5 7 9
7 10 13
9 13 17
11 16 21
13 19 25
15 22 29
17 25 33

But a better approach is to construct the 2d array, and write that with one savetxt call:

In [185]: output = np.array([2,3,4])*np.arange(9)[:,None]+1
In [186]: output
Out[186]: 
array([[ 1,  1,  1],
       [ 3,  4,  5],
       [ 5,  7,  9],
       [ 7, 10, 13],
       [ 9, 13, 17],
       [11, 16, 21],
       [13, 19, 25],
       [15, 22, 29],
       [17, 25, 33]])
In [187]: np.savetxt('txt', output, fmt='%10d')
In [188]: cat txt
         1          1          1
         3          4          5
         5          7          9
         7         10         13
         9         13         17
        11         16         21
        13         19         25
        15         22         29
        17         25         33
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This worked perfectly. I appreciate it. I did not want to generate the entire 2 array because this is being used to store x,y values from tracking objects in a video. So this file will grow very large. My understanding is that writing the values to an open text document would be faster. But the method you showed helped me write the values appropriately to the text file as I had wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.