zero fill empty spaces at specific locations
<p>I have a small question. I have a file in the following format: </p>

<pre><code>1 2 
1 2 3
1 2
1 2 3 4
2 4
</code></pre>

<p>The values in the code actually represent numbers(not necessarily single digit), but they can be any number, can be floating point values too. </p>

<p>Input file: For a particular row, each number is separated from another by a single whitespace (the separator can't be anything other than whitespace). </p>

<p>My task: I want to zero fill the empty spaces in such a way that it looks like this, i.e. fill up the empty spaces in such a way that it gives me a nice matrix-looking format:</p>

<pre><code>1 2 0 0
1 2 3 0
1 2 0 0
1 2 3 4
2 4 0 0
</code></pre>

<p>Output file: Same rule applies. For a particular row, each number is separated from another by a single whitespace only.</p>

<p>Language used: Python(or may be Shell, if that's possible)</p>

<p>I know there is this function called zfill, but I don't think that would be much of a help to me. </p>

<p>My solution: Find the (maximum length/2) of each line, using len and max functions. Then, using split(), fill up with zeros at appropriate places of each line. I am afraid it might turn into a dirty code and I'm sure there are better ways to accomplish this task. </p>

<p>Any suggestion is welcome. </p>

<p>Thank you!</p>
