[英]Print string in fixed format by 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"
但這打印如下:
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
我期望的所需格式:
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
每行有 9 個 string2 元素,並且元素與點對齊。
嘗試這個
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")
使用以下代碼:
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"
我可以這樣:
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
如果有人可以幫助按點對齊字符串元素,那將是完美的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.