2017-06-06 20 views
1

图片转移值:未对齐列:如何使用SQL时蟒蛇

请参考上面链接的图片。我有一封电子邮件报告,显示的是带有括号的负值。我需要做到这一点,每个号码中的最后一位数字排列在一起。在运行SQL语句时,通过使用for循环和if语句来打印值。这些值在运行SQL语句时打印出来。

我该如何获得正值以便左移与括号中最后一位数字对齐?

下面是用于打印在报告中列循环:

m1_total=0 
m3_total=0 
m6_total=0 
m12_total=0 


for line in curs: 
    if (line[1]==0) or (line[1]==None): #skip if the M1 is 0 
     continue 


    m1=0 
    m3=0 
    m6=0 
    m12=0 
    if line[1]>0: 
     m1=line[1] 
    if line[2]>0: 
     m3=round(line[2]/3) 
    if line[3]>0: 
     m6=round(line[3]/6) 
    if line[4]>0: 
     m12=round(line[4]/12) 
    mc3=0 
    mc6=0 
    mc12=0 
    if m3 > 0: 
     mc3=round((m1/m3)*100-100,2) 
    if m6>0: 
     mc6=round((m3/m6)*100-100,2) 
    if m12 > 0: 
     mc12=round((m6/m12)*100-100,2) 


    outStr+=str(line[0]).ljust(13,'.')+round_dolls(m1,9)+round_dolls(m3,9)+round_dolls(m6,9)+round_dolls(m12,9) 
    outStr+=round_dolls(mc3,14,'d')+round_dolls(mc6,9,'d')+round_dolls(mc12,9,'d') + '<br>' 
    m1_total+=m1 
    m3_total+=m3 
    m6_total+=m6 
    m12_total+=m12 
    mc3_total=0 
    mc6_total=0 
    mc12_total=0 
    if m3_total > 0: 
     mc3_total=round((m1_total/m3_total)*100-100,2) 
    if m6_total>0: 
     mc6_total=round((m3_total/m6_total)*100-100,2) 
    if m12_total > 0: 
     mc12_total=round((m6_total/m12_total)*100-100,2) 

    if line[-1] != ')': 
     str(m12) = str(m12)[:-1] 

最后两行是我试图实施该解决方案,但没有奏效。

下面是放负数括号中的方法:

def format_num(num,justLen,justWithChar='.',howManyDecimal=0,putComma='Y',minusFormat='-',zeroChar='0'): 
    if not num: 
     num = 0 

    if num == 0: 
     if zeroChar <> '0': 
      return zeroChar.rjust(justLen,justWithChar) 

    roundedNum = round(num, howManyDecimal) # rounded becomes float 
    if putComma=='Y': # put comma at thousand 
     numStr = '{:,}'.format(roundedNum) # fractional part is truncated to 5 decimal place 
    else: 
     numStr = str(roundedNum) 
    if howManyDecimal == 0: 
     numStr = numStr.rsplit('.')[0] # 1,234.99 -> ['1,234', '99'] 
    else: # to pad with 0 ex) 4234.9 -> 4234.90 
     numStr=numStr.rsplit('.')[0] + '.' + numStr.rsplit('.')[1].ljust(howManyDecimal, '0') 

    if num < 0: 
     if minusFormat=='P': # change - sign to parenthesis format 
      numStr = numStr.replace('-', '(') + ')' 

    return numStr.rjust(justLen,justWithChar) 

下面是从round_dolls()的代码:

def round_dolls(num, justLen, format='I', zeroChar='0'): 
    if format == 'Q': # quantity - no comma - 99999 
     return format_num(num, justLen, '.', 0, 'N','-',zeroChar) 
    elif format == 'I': # integer - 99,999 
     return format_num(num, justLen, '.', 0, 'Y','-',zeroChar) 
    elif format == 'F': # float wit 2 decimal places - 99,999.99 
     return format_num(num, justLen, '.', 2, 'Y','-',zeroChar) 
    elif format == 'D': # 99,999 negative number (99,999) 
     return format_num(num, justLen, '.', 0, 'Y','P',zeroChar) 
    elif format == 'd': # 99,999.99 negative number (99,999.99) 
     return format_num(num, justLen, '.', 2, 'Y','P',zeroChar) 
    elif format == 'P': # percentage 
     return format_num(num*100, justLen, '.', 0) + '%' 
    else: 
     return 'Format not specified' 
+0

凡在该代码被它改变负值使用圆括号? – Barmar

+0

如果数字是正数,则在后面打印一个空格。这将使最后一位数字与括号中最后一位数字一致。 – Barmar

+1

同时也减少之前的'.'数量为1. – Barmar

回答

1

变化:

if num < 0: 
    if minusFormat=='P': # change - sign to parenthesis format 
     numStr = numStr.replace('-', '(') + ')' 

到:

if minusFormat == 'P': 
    if num < 0: # change - sign to parenthesis format 
     numStr = numStr.replace('-', '(') + ')' 
    else: # add extra character to positive values to line up with negative 
     numStr = numStr + justWithChar 

如果你不希望在该行的最后一个字符.,则可以在for c in range(len(line))循环之后把这个删除字符,如果它不是)

if rowStr[-1] != ')': 
    rowStr = rowStr[:-1] 
+0

它的工作原理,但它增加了一个“。”在最后一列的末尾印刷数字,如“123.45”。 –

+0

那是因为'+ justWithChar'。难道不是所有的人都应该认为这些填充字符是值得的吗? – Barmar

+0

'format_num()'不知道它是行上的最后一个数字,所以它不能以不同的格式对其进行格式化。如果你找到调用它的代码,它可以在最后删除'.'。 – Barmar