Home > OS >  Output formatted 2D text with different number of columns and wrapping in Python
Output formatted 2D text with different number of columns and wrapping in Python

Time:06-22

I need to print a 2D array such as

a = [[100.0, 200.0, 300.0, 400.0, 500.0, 600.0],
     [700.0, 800.0, 900.0, 1000.0, 1100.0, 1200.0],
     [1300.0, 1400.0, 1500.0, 1600.0, 1700.0, 1800.0]]

to a text file with following format:

row=    1   1.00E 02    2.00E 02    3.00E 02    4.00E 02    5.00E 02
            6.00E 02    
row=    2   7.00E 02    8.00E 02    9.00E 02    1.00E 03    1.10E 03
            1.20E 03        
row=    3   1.30E 03    1.40E 03    1.50E 03    1.60E 03    1.70E 03
            1.80E 03    

Now if it was a simple table, there are many good posts and solutions already such as using print('{0:<8} {1:<8} {2:<8} {3:<8} {4:<8}'.format(...) or looping through the i lines and print(*i.split(","), sep = "\n") etc. But the the fact that elements in the array have to be wrapped in 5 columns (for example if the array was 4 x 14, the numbers for each row span three lines but not all the way) and also the fact that the first two columns are missing for some columns makes it to hard for me. I'd rather not use any fancy libraries but normal ones such as Numpy and Pandas are perfectly fine. The widths of the columns can be flexible, I have seen files such as following acceptable too, and the main limitation is 5 column wrapping:

 row=    1   1.50768831520556500E 03   1.61593623564084623E 03   1.72793908252278788E 03   1.84745178229805083E 03   1.89005237234217384E 03
             1.93242770965689124E 03   1.94285107599349976E 03   1.95428167992566091E 03   1.97250548659241736E 03   2.00154699718308871E 03
             2.03838485708810049E 03   2.07823624774815426E 03   2.13405634328665110E 03   1.95456774471011431E 03
 row=    2   1.44364188653061069E 03   1.52792163517318954E 03   1.61391596698350054E 03   1.70333946512927673E 03   1.73425508334279334E 03
             1.76507211642371681E 03   1.77245044015945177E 03   1.78083908332863643E 03   1.79497739305904815E 03   1.81799782545806261E 03
             1.84660527255144098E 03   1.87589773534454025E 03   1.93023915683827636E 03   1.91021293716667878E 03

CodePudding user response:

You can use list comprehension to split each row of a into sections of <= 5, and format all your numbers with {:.2E} for scientific notation.

This separates by 4 spaces too

with open('output.txt', 'w') as w:
    for i, row in enumerate(a, start=1):
        # Write start
        w.write(f'row=    {i}    ')
        # Get spaces for row overflow
        space = (12   len(str(i)))*' '
        # Split row into sections of 5
        tempa = [row[i:i 5] for i in range(0, len(row), 5)]
        # Write row and .join sections by newline   spaces
        w.write(
            f'\n{space}'.join(['    '.join([f'{a:.2E}' for a in b]) for b in tempa]))
        w.write('\n')
row=    1    1.00E 02    2.00E 02    3.00E 02    4.00E 02    5.00E 02
             6.00E 02
row=    2    7.00E 02    8.00E 02    9.00E 02    1.00E 03    1.10E 03
             1.20E 03
row=    3    1.30E 03    1.40E 03    1.50E 03    1.60E 03    1.70E 03
             1.80E 03

Display a decimal in scientific notation

How to divide a list into n equal parts, python

Feel free to simply add and delete spacing in the strings if necessary

  • Related