Aashish sharma Sharma
Aashish sharma Sharma

Reputation: 307

Missing one space in python string format

Hello I am am trying to format string and somehow I am missing 1 space. I know it may look trivial and some people may say its just 1 space but I want to see what am I missing.

file_out=open("Check.log","w")
file_out.write("{0:12} {1:20} {2:30}\n".format("TUPLE","LOGFILE STATUS","FSDB STATUS"))
file_out.write("{:12} {:20}".format((tuple_id+number),"Clean"))
file_out.write("{:30}\n".format("FSDB missing"))
file_out.write("{:12} {:20} {:30}\n".format(tuple_id,"Missing","Not Applicable"))

Please see this is just partial code just to explain the problem. The complete output is as follows

TUPLE        LOGFILE STATUS       FSDB STATUS                   
1234567_2    Clean               FSDB missing                  
1234556      Clean               FSDBs dumped successfully     
1234567_5    Not Clean           FSDB missing                  
1234567_4    Not Clean           FSDB missing                  
1234567_1    Clean               FSDBs dumped successfully     
1234567      Missing              Not Applicable    

I counted the spaces. In first line Starting from "T" to 2 spaces before "L" there are 12 spaces then 1 space then starts LOGFILE STATUS so in essence it gave placeholder for 12 characters then starts new word with 1 space. Same is true for LOGFILE STATUS and FSDB STATUS ie 20 spaces plus 1 space then it prints FSDB STATUS but As you can see rest of the lines there is just 20 spaces i.e between Clean and FSDB missing or the other table entries though I have same spacing for all

Upvotes: 2

Views: 605

Answers (1)

cs95
cs95

Reputation: 402882

This line:

file_out.write("{:12} {:20} ".format((tuple_id + number), "Clean")) 
# _________________________^ you're missing this one

Needs a space at the end, to be consistent with the other lines. I assume that line is in a loop.

Upvotes: 3

Related Questions