Python numpy method matrix.toFile() -


I need help formatting my matrix when I write it in the file I am using the numpy method Is called tofile () it takes 3 args. The file should have 1-name, 2-separator (string should be), 3-format (also a string) I do not know much about formatting, but I do not know the file I am trying to format, there are nine liners each new line (r No space included) Output is a 9x9 soduku game. So I need to format it 9x 9.

  expired = MOOfile ("soduku_solved.txt", "", "")   

where M is a matrix

My first argument is the name of the file, the second is a place, but I do not know what format logic it is necessary to create 9x9

I may be wrong, but I do not think it is possible with numpy tofile function. I think the format argument only allows you to format each object, it does not believe in any group.

You can do something like this:

  M = np.random.randint (1, 9, (9, 9)) each_itam_fmt = '{: & gt; 3} 'each_ro_fmt =' '.join ([every_item_fmt] * 9) fmt =' \ n'.join ([every_row_fmt] * 9) as_string = fmt.format (* M.flatten ())  < / Pre> 

This format is not a very good way to create a string and it is important to have a better way of doing so you will see the end result ( print (FMT) ) '{: & gt; ; 3} ', which basically says, enter a little bit of data here with a certain width of 3 characters, right-aligned.

Edit Since you are putting it directly in a file, you can write it line by line:

  m = np.random.randint (1: 9) , (9, 9)) fmt = ('{: & gt; 3}' * 9). With Open ('soduku_solved.txt', 'w') .writing (fmt.format (* m) + '\ n')    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -