-4

I have this format that I need to comply with in other to print a list of number (the list has over 200 numbers):

my_format = '%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d\n'
my_list = [1,2,3,4,5,6,199, 57, .........]

Basically, it is just %6d repeated 15 times.

When you use % to format a string, I know you need to do this:

with open('my_out.txt', 'w') as fout:
    fout.write(my_format % (num1, num2, num3, num4, num5, ....., num15)

In my case I would need to split my_list into 15 pieces and in the last piece, I may not have 15 numbers; therefore, I would need to somehow ignore the variables num that just have no item from my_list because I already iterated through all the numbers. Also, considering that I would need to create 15 variables (from num1 to num15), so I can use:

my_format % (num1, num2, num3, num4, num5, ....., num15)

My file should look like this:

0 1 2 3 4 5 6 7 8 9 1 1 2 1 1

1 4 5 8 9 7 5 8 9 7 6 5 4 3 0

3 4 6 7 1 8

Is there a smart and efficient way that I can solve my problem?

ananvodo
  • 371
  • 5
  • 13

4 Answers4

4

Just format the individual items with %6d, then join them on the empty string and add a newline to the end:

nums = list(range(15))
formatted = "".join("%6d" % n for n in nums) + "\n"

print(formatted)
#      0     1     2     3     4     5     6     7     8     9    10    11    12    13    14
Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • Can you please help me with an example where you use nums = list(range(20)). How to only take 15 in the first line and then the remaining 5 in the last line – ananvodo Dec 22 '20 at 01:18
  • @ananvodo https://stackoverflow.com/q/312443/5923139 – Aplet123 Dec 22 '20 at 01:19
  • If you only need the first 15 and not every 15, then just slice it: `formatted = "".join("%6d" % n for n in nums[:15]) + "\n"` – Aplet123 Dec 22 '20 at 01:20
  • I used the link you provided. I was able to get my_list into chunks of 15 items. When I run your code: formatted = "".join("%6d" % n for n in nums) + "\n", I get the error: TypeError: %d format: a number is required, not list – ananvodo Dec 22 '20 at 01:30
  • After you chunk the list, you'd have to loop the formatting onto each element. – Aplet123 Dec 22 '20 at 01:31
  • thanks for the solution. I posted the answer just using your solution. – ananvodo Dec 22 '20 at 01:40
2

Not a smart (or even pythonic) way but it gets the job done:

with open('my_out.txt', 'w') as fout:
    while len(my_list) > 0:
        str = ''
        for loop in range(0, 15):
            try:
                str += '%6d' % my_list.pop(0)
            except:
                break
        str += '\n'
        fout.write(str)
chengguan
  • 21
  • 3
0
def chunks(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

my_list = [f'{x:6d}' for x in list(range(300))]

for lst in list(chunks(my_list, 15)):
    print(' '.join(lst))

     0      1      2      3      4      5      6      7      8      9     10     11     12     13     14
    15     16     17     18     19     20     21     22     23     24     25     26     27     28     29
    30     31     32     33     34     35     36     37     38     39     40     41     42     43     44
    45     46     47     48     49     50     51     52     53     54     55     56     57     58     59
    60     61     62     63     64     65     66     67     68     69     70     71     72     73     74
    75     76     77     78     79     80     81     82     83     84     85     86     87     88     89
    90     91     92     93     94     95     96     97     98     99    100    101    102    103    104
   105    106    107    108    109    110    111    112    113    114    115    116    117    118    119
   120    121    122    123    124    125    126    127    128    129    130    131    132    133    134
   135    136    137    138    139    140    141    142    143    144    145    146    147    148    149
   150    151    152    153    154    155    156    157    158    159    160    161    162    163    164
   165    166    167    168    169    170    171    172    173    174    175    176    177    178    179
   180    181    182    183    184    185    186    187    188    189    190    191    192    193    194
   195    196    197    198    199    200    201    202    203    204    205    206    207    208    209
   210    211    212    213    214    215    216    217    218    219    220    221    222    223    224
   225    226    227    228    229    230    231    232    233    234    235    236    237    238    239
   240    241    242    243    244    245    246    247    248    249    250    251    252    253    254
   255    256    257    258    259    260    261    262    263    264    265    266    267    268    269
   270    271    272    273    274    275    276    277    278    279    280    281    282    283    284
   285    286    287    288    289    290    291    292    293    294    295    296    297    298    299
LetzerWille
  • 5,355
  • 4
  • 23
  • 26
0
def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

list_chunks = list(chunks(my_list, 15))

all_item = []
for element in x:
    formatted = "".join("%6d" % n for n in element) + "\n"
    all_item.append(formatted)

You can either make it as a list or directly print it to a file.

The solution was provided by @Aplet123. I am just putting all together

ananvodo
  • 371
  • 5
  • 13