python 格式化输出

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import with_statement

format print http://python.jobbole.com/82292/

for subprocess
self.cmd = (self.EXE + ” ” + self.PARAMS).split() if platform.system() == “Windows” else [self.EXE + ” ” + self.PARAMS]
subprocess.check_output(self.cmd, shell=True)

or:
self.cmd = (self.EXE + ” ” + self.PARAMS).split() # for Both Linux and Windows
subprocess.check_output(self.cmd, shell=False)

print( “%02d” % 10) # 0010
print(“{num:02d}”.format(num=1)) #01
str(1).zfill(5) # “00001”
str(1).rjust(5, ‘0’) #”00001″
str(1).ljust(5, ‘0’) #”10000″
str(1).center(5, ‘0’) #”00100″

format string output
“{val:%02d}”.format(val=10)
“{f:3.2f}”.format(f=f)
“{0:3.2f}”.format(3.2323)
“%3.2f”.format(3.2323)
“{f:2.4s}”.format(f=”adsfadsf”)

import math
#default
print “PI = %f” % math.pi
#width = 10,precise = 3,align = left
print “PI = %10.3f” % math.pi
#width = 10,precise = 3,align = rigth
print “PI = %-10.3f” % math.pi
#前面填充字符
print “PI = %06d” % int(math.pi)

#输出结果
#PI = 3.141593
#PI = 3.142
#PI = 3.142
#PI = 000003
#浮点数的格式化,精度、度和

#precise = 3
print “%.3s ” % (“jcodeer”)
#precise = 4
print “%.*s” % (4,”jcodeer”)
#width = 10,precise = 3
print “%10.3s” % (“jcodeer”)
#输出结果:
#jco
#jcod
# jco
#同于字符串也存在精度、度和。

import re
pattern = “.*-\d*\.ts”
if re.match(pattern, fn):
return False
else:
return True

print(“{tcost:<6.2f}sec cost for heatmap: i={i: <4d} t={t: <6.2f}”.format(tcost=tcost, t=t, i=i))

python: numpy

pip3 install jupyter

ipython or jupyter notebook
“`

import numpy as np

from matplotlib.pyplot import imshow, title
import matplotlib.pylot as plt
from scipy.misc import imread, imsave, imresize

xs = np.arange(-np.pi, np.pi, 0.01)
xs = np.linspace(-np.pi, np.pi, 100)

xs, ys = np.meshgrid(xs, xs)
xs = np.arange(12).reshape(3, 4)
mat = np.random.random((2, 3))

z = np.sqrt(xs ** 2 + ys ** 2)
plt.imshow(z); plt.colorbar()
plt.title(“pic”)
plt.show()
“`

refs:
– http://cs231n.github.io/python-numpy-tutorial/