简体   繁体   English

通过 Python 代码以固定格式打印字符串

[英]Print string in fixed format by Python code

I want to print a very large string or array in fixed format in order to compare with data file.我想以固定格式打印一个非常大的字符串或数组,以便与数据文件进行比较。 Here are my string and python code:这是我的字符串和 python 代码:

import re

string1 ='2222//000000'
string2 = '-0.0000023   0.0000008   0.0000001   0.9716336  -0.1052695  -0.0000001  -0.0000002   0.0000007  -0.0000000 \
            0.0000000   0.0000000   0.1316705   0.1175425  -0.0000000   0.0000000   0.0000000   0.0000000   0.0000000 \
            0.0000000   0.0000000   0.0000003   0.0000000  -0.0000001  -0.0000000   0.0000000  -0.0000000   0.0000001 \
            0.0000000  -0.0000003   0.0000000  -0.0000000  -0.0000001  -0.0000001   0.0106146   0.1165349'

split = re.findall(r'(?:\b|-)\d+(?:\.\d+)?', string2)

for i in range (3):
    print  "{0:<18}".format( string1 ),
    for j in range ( len(split) ):
        print "{0:<10}".format(split[j]),
        if j != 0 and j%9 == 0:
            print "\n", "{0:<18}".format("      " ),
    print  "\n"

But this print out as follows:但这打印如下:

2222//000000       -0.0000023 0.0000008  0.0000001  0.9716336  -0.1052695 -0.0000001 -0.0000002 0.0000007  -0.0000000 0.0000000  
                   0.0000000  0.1316705  0.1175425  -0.0000000 0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  
                   0.0000000  0.0000003  0.0000000  -0.0000001 -0.0000000 0.0000000  -0.0000000 0.0000001  0.0000000  
                   -0.0000003 0.0000000  -0.0000000 -0.0000001 -0.0000001 0.0106146  0.1165349  

The desired format I expect:我期望的所需格式:

2222//000000          -0.0000023   0.0000008   0.0000001   0.9716336  -0.1052695  -0.0000001  -0.0000002   0.0000007  -0.0000000
                       0.0000000   0.0000000   0.1316705   0.1175425  -0.0000000   0.0000000   0.0000000   0.0000000   0.0000000
                       0.0000000   0.0000000   0.0000003   0.0000000  -0.0000001  -0.0000000   0.0000000  -0.0000000   0.0000001
                       0.0000000  -0.0000003   0.0000000  -0.0000000  -0.0000001  -0.0000001   0.0106146   0.1165349

Each line has nine elements of string2, and the elements aligned with the dot.每行有 9 个 string2 元素,并且元素与点对齐。

try this尝试这个

import re

string1 ='2222//000000'
string2 = '-0.0000023   0.0000008   0.0000001   0.9716336  -0.1052695  -0.0000001  -0.0000002   0.0000007  -0.0000000 \
            0.0000000   0.0000000   0.1316705   0.1175425  -0.0000000   0.0000000   0.0000000   0.0000000   0.0000000 \
            0.0000000   0.0000000   0.0000003   0.0000000  -0.0000001  -0.0000000   0.0000000  -0.0000000   0.0000001 \
            0.0000000  -0.0000003   0.0000000  -0.0000000  -0.0000001  -0.0000001   0.0106146   0.1165349'

split = re.findall(r'(?:\b|-)\d+(?:\.\d+)?', string2)
print string1,
for j in range ( len(split) ):
    print("{0:<7}".format(split[j])),
    if j%9 == 0:
        print("\n")

With the following code:使用以下代码:

string1 ='2222//000000'

string2 = '-0.0000023   0.0000008   0.0000001   0.9716336  -0.1052695  -0.0000001  -0.0000002   0.0000007  -0.0000000 \
            0.0000000   0.0000000   0.1316705   0.1175425  -0.0000000   0.0000000   0.0000000   0.0000000   0.0000000 \
            0.0000000   0.0000000   0.0000003   0.0000000  -0.0000001  -0.0000000   0.0000000  -0.0000000   0.0000001 \
            0.0000000  -0.0000003   0.0000000  -0.0000000  -0.0000001  -0.0000001   0.0106146   0.1165349'

split = re.findall(r'(?:\b|-)\d+(?:\.\d+)?', string2)

print  split

for i in range (3):
    print  "{0:<18}".format( string1 ),
    for j in range ( len(split) ):
        print "{0:<12}".format(split[j]),
        if  (j+1) >= 9 and (j+1)%9 == 0:
            print "\n", "{0:<18}".format("      " ),
    print  "\n"

I can get like this:我可以这样:

2222//000000       -0.0000023   0.0000008    0.0000001    0.9716336    -0.1052695   -0.0000001   -0.0000002   0.0000007    -0.0000000   
                   0.0000000    0.0000000    0.1316705    0.1175425    -0.0000000   0.0000000    0.0000000    0.0000000    0.0000000    
                   0.0000000    0.0000000    0.0000003    0.0000000    -0.0000001   -0.0000000   0.0000000    -0.0000000   0.0000001    
                   0.0000000    -0.0000003   0.0000000    -0.0000000   -0.0000001   -0.0000001   0.0106146    0.1165349    

It will be perfect, if anyone can help on aligning the string elements by dot.如果有人可以帮助按点对齐字符串元素,那将是完美的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM