2013-12-12 200 views
1

我需要打印一些字符串周圍的一些標頭,你可以看到它在這裏工作正常,但如果字符串很長,我需要分割它然後打印一個更長的標題。分割一個字符串打印

+===================================================+ 
| Running: sdt_test         | 
| Skipping:inquiry/"Inq VPD C0" mem/"Maint In Cmnd" | 
+===================================================+ 
sh: /net/flanders/export/ws/ned/proto/bin/sdt_test: No such file or directory 
+=====================+ 
| Running: dtd_tester | 
+=====================+ 
sh: /net/flanders/export/ws/ned/proto/bin/dtd_tester: No such file or directory 
+===============+ 
| Running: pssm | 
+===============+ 
sh: /net/flanders/export/ws/ned/proto/bin/pssm: No such file or directory 
+==============+ 
| Running: psm | 
+==============+ 
sh: /net/flanders/export/ws/ned/proto/bin/psm: No such file or directory 
+===============================================================================================================================================================================================================================================================================================================================================+ 
| Running: ssm                                                                                 | 
| Skipping:"Secondary Subset Manager Tests"/"SSSM_3 Multi Sequence" "Secondary Subset Manager Tests"/"SSSM_2 Steady State" "Secondary Subset Manager Tests"/"SSSM_4 Test Abort" "Secondary Subset Manager Tests"/"SSSM_6 Test extend" "Secondary Subset Manager Tests"/"SSSM_9 exceptions" "Secondary Subset Manager Tests"/"SSSM_11 failed io" | 
+===============================================================================================================================================================================================================================================================================================================================================+ 

看起來精緻,雖然那裏的SSM測試,我想就一定數目的字符,分手也許100或只是套房之間的空白。

我真的不太確定如何做到這一點,這是目前這樣做的代碼。

#calculate lengths to make sure header is correct length 
     l1 = len(x) 
     l2 = len(y) 
     skip = False 
     if 'disable=' in test and 'disable="*"' not in test: 
      skip = True 
     #if entire test suite is to be disabled or not run 
     if disable: 
      headerBreak ="+" + "="*(l1+12) + "+" 
      print headerBreak 
      print "| Skipping: %s |" % x 
     #if the test suite will be executed 
     else: 
      if skip == False: 
       l2 = 0 
      headerBreak = "+" + "="*(max(l1,l2)+11) + "+" 
      print headerBreak 
      print "| Running: %s" % x, ' '*(l2-l1)+ '|' 
      #if some suites are disabled but some are still running 
      if skip: 
       print "| Skipping:%s |" % y 
     print headerBreak 
     sys.stdout.flush() 

回答

3

可以使用textwrap模塊來簡化這個

例如,如果最大寬度爲44

>>> max_width = 44 
>>> header='''Skipping:"Secondary Subset Manager Tests"/"SSSM_3 Multi Sequence" "Secondary Subset Manager Tests"/"SSSM_2 Steady State" "Secondary Subset Manager Tests"/"SSSM_4 Test Abort" "Secondary Subset Manager Tests"/"SSSM_6 Test extend" "Secondary Subset Manager Tests"/"SSSM_9 exceptions" "Secondary Subset Manager Tests"/"SSSM_11 failed io"''' 
>>> h = ["Running: ssm"] + textwrap.wrap(header, width=max_width-4) 
>>> maxh = len(max(h, key=len)) 
>>> print "+=" + "="*maxh + "=+" 
+==========================================+ 
>>> for i in h: 
...  print "| " + i.ljust(maxh) + " |"... 
| Running: ssm        | 
| Skipping:"Secondary Subset Manager  | 
| Tests"/"SSSM_3 Multi Sequence"   | 
| "Secondary Subset Manager Tests"/"SSSM_2 | 
| Steady State" "Secondary Subset Manager | 
| Tests"/"SSSM_4 Test Abort" "Secondary | 
| Subset Manager Tests"/"SSSM_6 Test  | 
| extend" "Secondary Subset Manager  | 
| Tests"/"SSSM_9 exceptions" "Secondary | 
| Subset Manager Tests"/"SSSM_11 failed | 
| io"          | 
>>> print "+=" + "="*maxh + "=+" 
+==========================================+ 
+0

這是非常有用的,但我發現它非常難這個翻譯成我的實際代碼。 –