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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.