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

发布于 2024-11-27 01:40:01 字数 725 浏览 0 评论 0原文

我怎样才能从中获取数据

 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   

谢谢..

How can I get data from this

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

to output like this

100             Aktiva     

    110         Aktiva Lancar                
        111     Kas                          
        112     Piutang Usaha                
        113     Piutang Gaji    

    120         Aktiva Tetap / Inventaris    
        121     Tanah                        
        122     Kendaraan  

200             Pasiva   

Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坦然微笑 2024-12-04 01:40:01

不知道数据来自哪里,如果您可以发布您的代码,那么我可以准确地帮助您找到您想要的东西,所以现在它只是猜测..如果您正在打印的数据来自某个循环,那么您可以这样做这..

   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

don't know where the data is coming from, if you can post your code then i can exactly help you what you are looking for so for now its just guess.. if the data you are printing coming from some loop then you can do this..

   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
怎会甘心 2024-12-04 01:40:01
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
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
辞取 2024-12-04 01:40:01

以下示例对我来说效果很好:

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

The following sample works fine for me:

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
英雄似剑 2024-12-04 01:40:01
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)
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)

result

100            Aktiva

    110        Aktiva Lancar
        111    Kas
        112    Piutang Usaha
        113    Piutang Gaji

    120        Aktiva Tetap / Inventaris
        121    Tanah
        122    Kendaraan

200            Pasiva

.

or

.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文