python - Formatting a numpy array as aligned text columns -


i want set character length of each element of numpy array written in text file

the output getting is:

99941     1 56765 56767 51785 51793     0     0     0     0  101150      1  59006  59005  51782  51783      0      0      0      0 

as see in above case numbers increase columns shifted.

but in ideal case output should like:

 99941      1  56765  56767  51785  51793      0      0      0      0  101150      1  59006  59005  51782  51783      0      0      0      0 

is there way can fix character length of each element of numpy array writes elements right left after considering element character length , keep column formatting fixed?

this code snippet on working.

def calculate(self, elementnum1, elementnum2):     angle = function().transformation(elementnum1, elementnum2)     elementinfo = shell().getelement(elementnum2)     card1 = np.array([0,0,0,0,0,0,0,0,0,0])     card1.itemset(0,(elementinfo[0]))     card1.itemset(1,(elementinfo[1]))     card1.itemset(2,(elementinfo[2]))     card1.itemset(3,(elementinfo[3]))     card1.itemset(4,(elementinfo[4]))     card1.itemset(5,(elementinfo[5]))     return str(card1)  def anglesolution1(self,elementpair):     pair = np.genfromtxt(elementpair, dtype=int, comments='none', delimiter=', ')     row = int(pair.size/2)     p = mp.pool(processes=4)     result = [p.apply_async(anglecalculatefunction().calculate, args=(pair[i][0], pair[i][1])) in range(0, int(row/4))]     result = open('angles.csv', "a")     result.write('\n'.join(map(str, ((((p.get()).replace('[','')).replace(']','')) p in result))))     result.write('\n')     result.close()     p.close() 

there performance issues using multiprocessing incorrectly, out of scope of discussion.

np.savetxt formats 1 row @ time, , writes open file.

in [536]: x out[536]:  array([[ 99941,      1,  56765,  56767,  51785,  51793,      0,      0,              0,      0],        [101150,      1,  59006,  59005,  51782,  51783,      0,      0,              0,      0]]) 

in effect, doing (using print instead of file write of illustration purposes):

in [537]: fmt=' '.join(['%8d']*x.shape[1]) in [538]: row in x:     print(fmt%tuple(row))    .....:         99941        1    56765    56767    51785    51793        0        0        0        0   101150        1    59006    59005    51782    51783        0        0        0        0 

or if you'd collect lines in 1 string, append them list:

in [544]: astr = [] in [545]: row in x:     astr.append(fmt%tuple(row))    .....:      in [546]: print('\n'.join(astr))    99941        1    56765    56767    51785    51793        0        0        0        0   101150        1    59006    59005    51782    51783        0        0        0        0 

python object display (str(...)) routinely sort of line append , join.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -