拆分字符串进行打印

时间:2013-12-12 16:21:09

标签: python python-2.7

我需要在某些字符串周围打印一些标题,你可以看到它在这里运行正常,但如果字符串很长我需要拆分它然后打印更长的标题。

+===================================================+
| 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()

1 个答案:

答案 0 :(得分: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 + "=+"
+==========================================+