0

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:

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.

5
  • is this python 2? Commented Jan 7, 2021 at 6:20
  • 1
    Try a multiline string: """your string""" Commented Jan 7, 2021 at 6:22
  • yes, I think so Commented Jan 7, 2021 at 6:23
  • @Alec No need to ask. It is Python 2. The print statement would be a syntax error in Python 3. Commented Jan 7, 2021 at 7:52
  • I asked because of the print... haven’t seen python 2 in ages Commented Jan 7, 2021 at 7:53

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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")

3 Comments

Thanks for the magic line, "if j%9 == 0", this makes the code better.
I figure out one improved code as posted, but still not perfectly align by dot of each elements
add another condition to check if the number is negative remove an extra space in the format statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.