Writing white-space delimited text to be human readable in Python #2 -


I want to write a 2D numerical array in a human-readable text file format. I had asked this question before but it only specifies the number of similar places to be associated with each element in the array. In it all the elements are localized with 10 spaces. What do I want in my array to have different places for each column.

For example, I want 7 places for my 1 column, 10 spaces for my 2 column, 4 spaces for my 3 columns, etc. What is the similarity to numpy.savetxt (file name, x, delimiter = ',', fmt = '% -10s'), but instead of'% -10 ', click'% -7s,% -10s,% -4s' etc. Where?

Thanks

Here's an example of how it might look (Python 2 & Amp;; 3):

  l = [[1,2,3,4], [3,4,5,6] for the row in line: Print (U ' {: & Lt; 7} {: & gt; 7} {: ^ 7} {: * ^ 7} '. Format (* line)) 1 2 3 *** 4 *** 3 4 5 *** 6 ***   

formatting options

  & gt; & Gt; & Gt; Is taken from '{: & Lt; 30} 'format (' Left Alliance ')' Left Alliance '& gt; & Gt; & Gt; '{: & Gt; 30} 'format (' right align ')' align right '& gt; & Gt; & Gt; '{: ^ 30}' format ('centric') 'centric' & gt; & Gt; & Gt; '{: * ^ 30}' Fill the '*' ********** ************************************************************ If a file is required, then do this:  
  open with '[[1,2,3,4], [3,4,5, 6]]' (file. Txt ',' wb ') as f: f.write (u' \ ufeff'.encode ('utf-8')) for the row in line: line = u '{: & lt; 7} {: & gt; 7} {: ^ 7} {: * ^ 7} \ r \ n '. Format (* line) f.write (line.incode ('UTF-8'))   

File contents

  1 2 3 *** 4 *** 3 4 5 *** 6 ***   

And the encoding is UTF-8 which means that you can have numbers not only, but anyone in your title The letter may be:

  title = [u'â ?? 'U', '' ',' ',' ',' '' '' ',' ',' Ufeff'.encode ('', '', '' with 'file.txt', 'wb') as 'F' 'Utf-8')) line = '{:: & lt; 7} {: & gt; 7} {: ^ 7} {: * ^ 7} \ r \ n '. Format (* Title) L. line = '{: & lt; 7} {: & gt; 7} {: ^ 7} {: * ^ 7} \ r \ n f.write for line in line (line.encode ('utf-8')) '.format (* line) f.write (line. Encode ('utf-8')) ?? a???? a???? *** One ?? ¡*** 1 2 3 *** 4 *** 2 3 4 *** 5 ***    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -