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.
"""your string"""printstatement would be a syntax error in Python 3.