2017-06-06 22 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