以特定格式python写入数据输出

时间:2011-08-01 09:39:12

标签: python string tuples

如何从此

获取数据
 100   Aktiva                       
 110   Aktiva Lancar                
 111   Kas                          
 112   Piutang Usaha                
 113   Piutang Gaji                 
 120   Aktiva Tetap / Inventaris    
 121   Tanah                        
 122   Kendaraan   

像这样输出

100             Aktiva     

    110         Aktiva Lancar                
        111     Kas                          
        112     Piutang Usaha                
        113     Piutang Gaji    

    120         Aktiva Tetap / Inventaris    
        121     Tanah                        
        122     Kendaraan  

200             Pasiva   

谢谢..

4 个答案:

答案 0 :(得分:2)

data = [
  (100, 'Aktiva'),
  (110, 'Aktiva Lancar'),
  (111, 'Kas'),
  (112, 'Piutang Usaha'),
  (113, 'Piutang Gaji'),
  (120, 'Aktiva Tetap / Inventaris'),
  (121, 'Tanah'),
  (122, 'Kendaraan'),
  (200, 'Pasiva') ]

def rzeros(a):
    ''' count number of zeros from right '''
    return len(a)-len(a.strip('0'))

def pads(a):
    ''' calculate left and right paddings '''
    la = len(a)
    rz = rzeros(a)
    return [(la-rz-1) * (la+1), rz * (la+1)]

for k,v in data:
    lpad, rpad = pads(str(k))
    print " "*lpad, k, " "*rpad, v

答案 1 :(得分:1)

不知道数据的来源,如果你可以发布你的代码,那么我可以完全帮助你找到你想要的东西,所以现在只是猜测..如果你打印的数据来自某个循环然后你可以这样做..

   for val in data:
       intVal = val[0]
       strVal = val[1]
       if intVal % 100 == 0: \\ for 100, 200, ...
          print str(intVal)+"\t\t\t"+strVal
       elif intVal % 10 == 0:  \\ for 110, 210, 220 ...
          print "\t"+str(intVal)+"\t\t"+strVal
       else:
          \\for 111, 112, ...
          print "\t\t"+str(intVal)+"\t"+strVal

答案 2 :(得分:0)

以下 示例 对我来说很好用:

data = [ (100, 'Aktiva'), (110, 'Aktiva Lancar'), (111, 'Kas'), (112, 'Piutang Usaha'), (113, 'Piutang Gaji'), (120, 'Aktiva Tetap / Inventaris'), (121, 'Tanah'), (122, 'Kendaraan'), (200, 'Pasiva'),(220, 'Pasiva1') ]

for k,v in data:
    pad = 0 if k % 100 == 0 else 1
    if (k - 100) % 10 != 0:
        pad += 1
    print '\t' * pad + str(k) + '\t' + v

答案 3 :(得分:0)

ss = '''100   Aktiva
110   Aktiva Lancar
111   Kas
112   Piutang Usaha
113   Piutang Gaji
120   Aktiva Tetap / Inventaris
121   Tanah
122   Kendaraan
200             Pasiva'''

import re
regx = re.compile('^(\d+)[ \t]+(.*)',re.MULTILINE)

maxi = max(len(number) for number,content in regx.findall(ss))
sp = (maxi+1)*' '

for number,content in regx.findall(ss):
    z = len(number.rstrip('0'))-1
    if number.endswith('0'):  print
    print '%s%s%s%s' % (z*sp, number, (maxi-z)*sp,   content)

结果

100            Aktiva

    110        Aktiva Lancar
        111    Kas
        112    Piutang Usaha
        113    Piutang Gaji

    120        Aktiva Tetap / Inventaris
        121    Tanah
        122    Kendaraan

200            Pasiva

data = [ (100, 'Aktiva'),
         (110, 'Aktiva Lancar'),
         (111, 'Kas'),
         (112, 'Piutang Usaha'),
         (113, 'Piutang Gaji'),
         (120, 'Aktiva Tetap / Inventaris'),
         (121, 'Tanah'),
         (122, 'Kendaraan'),
         (200, 'Pasiva') ]

maxi = len(str(max(n for n,content in data)))
sp = (maxi+1)*' '

for n,content in data:
    z = len(str(n).rstrip('0'))-1
    if n%10==0:  print
    print '%s%s%s%s' % (z*sp, n, (maxi-z)*sp,   content)