Python 标准化输出

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 标准化输出相关的知识,希望对你有一定的参考价值。

参考技术A

Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。相对于老版的%格式方法,它有很多优点。
1.在%方法中%s只能替代字符串类型,而在format中不需要理会数据类型;
2.单个参数可以多次输出,参数顺序可以不相同;
3.填充方式十分灵活,对齐方式十分强大;
4.官方推荐用的方式,%方式将会在后面的版本被淘汰。

1、按照默认顺序, 不指定位置

2、设置 指定位置 ,可以多次使用

3、使用 字典 格式化

4、通过 列表 格式化

填充与对齐 ^ < > 分别代表居中,左对齐,右对齐,后面数字表示宽度,\':\' 后面带填充的字符,只能是一个字符,不指定的话默认空格.

有时间写一下这个例子

暂时先放两个回头再补

用“,”还能做金额的千位分隔符。

python调用外部命令

python调用外部命令


os.system:  输出在终端上,捕捉不到

os.popen:  只能捕捉到标准输出,捕捉不到标准错误输出

os.popen2: 返回2个对象,一个是标准输入,一个是标准输出

os.popen3: 返回3个对象,标准输入,标准输出,标准错误输出

os.popen4: 返回2个对象,pip_in 和pip_out_err



os.system:  输出在终端上,捕捉不到

In [4]: os.system(‘ls‘)                                                                                                                                                            
13001195917  1_md5.py  1_walk.py  2.txt  ErrorExcept.py  hashlib2.py  test  top10.py  walk1.py  yield1.py  yield2.py  非码+支付宝会员_喜欢牛肉_手机号码.txt                        
Out[4]: 0  

In [6]: s = os.system(‘ls‘)                                                                                                                                                        
13001195917  1_md5.py  1_walk.py  2.txt  ErrorExcept.py  hashlib2.py  test  top10.py  walk1.py  yield1.py  yield2.py  非码+支付宝会员_喜欢牛肉_手机号码.txt                        

In [7]: s                                                                                                                                                                          
Out[7]: 0     #echo $? 返回值为0,                                                                                                                                                                     

In [9]: s = os.system(‘ls a‘)                                                                                                                                                      
ls: 无法访问a: 没有那个文件或目录                                                                                                                                                  

In [10]: s                                                                                                                                                                         
Out[10]: 512  #echo $? 返回值为512



os.popen:  只能捕捉到标准输出,捕捉不到标准错误输出

In [12]: out = os.popen(‘ls‘)                                                                                                                                                      
                                                                                                                                                                                   
In [13]: out.read()                                                                                                                                                                
Out[13]: ‘13001195917\n1_md5.py\n1_walk.py\n2.txt\nErrorExcept.py\nhashlib2.py\ntest\ntop10.py\nwalk1.py\nyield1.py\nyield2.py\n\xe9\x9d\x9e\xe7\xa0\x81+\xe6\x94\xaf\xe4\xbb\x98\x
e5\xae\x9d\xe4\xbc\x9a\xe5\x91\x98_\xe5\x96\x9c\xe6\xac\xa2\xe7\x89\x9b\xe8\x82\x89_\xe6\x89\x8b\xe6\x9c\xba\xe5\x8f\xb7\xe7\xa0\x81.txt\n‘



os.popen2: 返回2个对象,一个是标准输入,一个是标准输出

In [23]: stdin, stdout = os.popen2(‘sort‘)                                                                                                                                         
/usr/local/bin/ipython:1: DeprecationWarning: os.popen2 is deprecated.  Use the subprocess module.                                                                                 
  #!/usr/local/bin/python2.7

In [24]: stdin.write(‘hello‘)     #stdin输入hello                                                                                                                                                 
                                                                                                                                                                                   
In [25]: stdin.write(‘world‘)                                                                                                                                                      
                                                                                                                                                                                   
In [26]: stdin.write(‘abc\n‘)                                                                                                                                                      
                                                                                                                                                                                   
In [27]: stdin.write(‘123\n‘)                                                                                                                                                      
                                                                                                                                                                                   
In [28]: stdin.close()                                                                                                                                                             
                                                                                                                                                                                   
In [29]: stdout.read()         #stdout.read()会把stdin()的标准输入排序输出                                                                                                                                                       
Out[29]: ‘123\nhelloworldabc\n‘



os.popen3: 返回3个对象,标准输入,标准输出,标准错误输出

In [33]: stdin, stdout, stderr = os.popen3(‘ls a‘)                                                                                                                                 
/usr/local/bin/ipython:1: DeprecationWarning: os.popen3 is deprecated.  Use the subprocess module.                                                                                 
  #!/usr/local/bin/python2.7

In [34]: stderr.read()      #执行stderr报错                                                                                                                                                       
Out[34]: ‘ls: \xe6\x97\xa0\xe6\xb3\x95\xe8\xae\xbf\xe9\x97\xaea: \xe6\xb2\xa1\xe6\x9c\x89\xe9\x82\xa3\xe4\xb8\xaa\xe6\x96\x87\xe4\xbb\xb6\xe6\x88\x96\xe7\x9b\xae\xe5\xbd\x95\n‘


os.popen4: 返回2个对象,pip_in 和pip_out_err(标准输出和标准错误输出保存到一个输出pip_out_err)



subprocess模块

In [43]: import subprocess  
In [50]: subprocess.call([‘ls abcd.txt‘])
OSError: [Errno 2] No such file or directory    

In [10]: subprocess.call([‘ls -l‘])  #会把-l当成一个文件,找不到该文件
OSError: [Errno 2] No such file or directory    
 
In [49]: subprocess.call([‘ls -l‘],shell = True)  #默认shell=False ,shell=True,表示执行ls -l命令                                                                                                                                
总用量 2888                                                                                                                                                                        
-rw-r--r-- 1 root root      12 12月  6 19:50 13001195917
-rw-r--r-- 1 root root     453 12月  6 19:31 1_md5.py
-rw-r--r-- 1 root root     453 12月  2 16:48 1_walk.py
-rw-r--r-- 1 root root      14 12月  6 16:42 2.txt

In [51]: help(subprocess.call) 
call(*popenargs, **kwargs)     #表示接受冗余参数

In [54]: subprocess.call([‘ls‘,‘-l‘])   #或者写入列表中                                                                                                                                           
总用量 2888                                                                                                                                                                        
-rw-r--r-- 1 root root      12 12月  6 19:50 13001195917
-rw-r--r-- 1 root root     453 12月  6 19:31 1_md5.py
-rw-r--r-- 1 root root     453 12月  2 16:48 1_walk.py
-rw-r--r-- 1 root root      14 12月  6 16:42 2.txt


In [58]: a=subprocess.check_call([‘ls‘,‘-l‘])                                                                                                                                      
总用量 2888                                                                                                                                                                        
-rw-r--r-- 1 root root      12 12月  6 19:50 13001195917
-rw-r--r-- 1 root root     453 12月  6 19:31 1_md5.py
-rw-r--r-- 1 root root     453 12月  2 16:48 1_walk.py
-rw-r--r-- 1 root root      14 12月  6 16:42 2.txt
-rw-r--r-- 1 root root     412 12月  7 20:25 ErrorExcept.py
-rw-r--r-- 1 root root     425 10月 24 05:57 hashlib2.py
drwxr-xr-x 2 root root    4096 10月 24 06:03 test
-rw-r--r-- 1 root root     556 12月  6 16:06 top10.py
-rw-r--r-- 1 root root     430 10月 25 21:56 walk1.py
-rw-r--r-- 1 root root     128 10月 25 18:07 yield1.py
-rw-r--r-- 1 root root     680 10月 25 21:27 yield2.py
-rw-r--r-- 1 root root 2910024 12月  6 16:48 非码+支付宝会员_喜欢牛肉_手机号码.txt

In [59]: a                                                                                                                                                                         
Out[59]: 0    

In [62]: a=subprocess.check_call([‘ls‘,‘a‘])    #抛出异常                                                                                                                                   
ls: 无法访问a: 没有那个文件或目录   
---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
<ipython-input-62-575484aab70a> in <module>()
----> 1 a=subprocess.check_call([‘ls‘,‘a‘])

/usr/local/lib/python2.7/subprocess.pyc in check_call(*popenargs, **kwargs)
    538         if cmd is None:
    539             cmd = popenargs[0]
--> 540         raise CalledProcessError(retcode, cmd)
    541     return 0
    542 

CalledProcessError: Command ‘[‘ls‘, ‘a‘]‘ returned non-zero exit status 2
In [63]: a     #a的值还是0,并没有将异常的值保存到a中                                                                                                                                                                    
Out[63]: 0   

[[email protected] systeminformation]# vim process_check_call.py 
#!/usr/bin/env python                                                                                                                                                              
#ecoding=utf8                                                                                                                                                                      
                                                                                                                                                                                   
import subprocess                                                                                                                                                                  
                                                                                                                                                                                   
                                                                                                                                                                                   
try:                                                                                                                                                                               
    subprocess.check_call(‘exit 1‘, shell = True)                                                                                                                                  
except Exception:                                                                                                                                                                  
    print "exit 1返回echo $?不是0,抛出异常,打印出来" 
[[email protected] systeminformation]# python process_check_call.py
exit 1返回echo $?不是0,抛出异常,打印出来



#!/usr/bin/env python                                                                                                                                                              
#ecoding=utf8                                                                                                                                                                      
                                                                                                                                                                                   
import subprocess                                                                                                                                                                  
                                                                                                                                                                                   
                                                                                                                                                                                   
try:                                                                                                                                                                               
    subprocess.check_call(‘exit 1‘, shell = True)                                                                                                                                  
except subprocess.CalledProcessError:      #不知道异常名称,使用:except Exception:  表示接受所有异常处理                                                                                                                                        
    pass                                                                                                                                                                           
print "exit 1返回echo $?不是0,抛出异常,打印出来,捕捉到异常"     

[[email protected] systeminformation]# python process_check_call.py
exit 1返回echo $?不是0,抛出异常,打印出来,捕捉到异常





本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1880494

以上是关于Python 标准化输出的主要内容,如果未能解决你的问题,请参考以下文章

在 python 中控制 ruby​​ 程序的标准输入和标准输出

[python标准库]Logging模块

python中有多种标准输出格式,以下四种为常见的4种标准输出格式

在 Python 中流式传输标准输入/标准输出

如何使用 Python 将标准输入/标准输出通过管道传输到 Perl 脚本

Python 运行守护程序子进程并读取标准输出