在python中使用SQL时如何移位

2024-09-28 22:35:28 发布

您现在位置:Python中文网/ 问答频道 /正文

图片:未对齐的列:

请参考上面链接的图片。我有一封电子邮件报告,上面写着负值,周围有圆括号。我要让每个数字的最后一个数字排成一行。在运行SQL语句时,使用for循环和if语句打印这些值。这些值在SQL语句中运行时被打印出来。你知道吗

如何使正值左移一次,与括号中数字的最后一位对齐?你知道吗

下面是打印报表中列的for循环:

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\u 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'

Tags: formatreturniflinenumm3totalround
1条回答
网友
1楼 · 发布于 2024-09-28 22:35:28

更改:

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]

相关问题 更多 >