python中常用的知识

python中一切事务皆为对象。

所以我们看字符串、数字、集合等全部使用类的方法查看某一个对象。

a = 'sb,2b'

查看对象是什么类型:print(type(a))

查看此对象有哪些属性:dir(a)

查看此对象有哪些属性也可以用:vars(a),不仅能看到所有属性还会给属性标明是方法还是属性,也可以看到是方法

查看类下的方法如何使用: help(a)

字符串的方法如: a = 'sb,2b' ,

a.upper() 将字符全部大写

a.split(',') 将字符串处理称字典

a.__contains__(‘er’) 包含返回true 语法堂 ‘er’ in a
a.__contains__(‘er’) 包含返回true 语法堂 ‘er’ in a
a.__eq__() 字符串的相等 语法堂 ==
__format__ 字符串的格式化,
  >>>a = ‘eric{0}’
  >>>a.__format__(‘alex’)
  >>> a.format('alex’) 
  ‘alexalex'
a.capitalize()首字符大写了
  >>> a.capitalize()
   ‘Alex’
a.casefold() 将大写字符变小些
  >>> a = 'alExV5'
  >>> a.casefold()
  ‘alexv5'
a.center这个有用
  >>> print(8*'*',a,8*'*')
  ******** alExV5 ********用center可以实现
  >>> a.center(20) 
  '         alExV5         ‘ 
  >>> a.center(20,'*’)
   '*******alExV5*******’
a.count(‘a')计算字符出现的次数。还可以定义起始位置和结束位置
a.count(‘a’,0,10) 从位置0到为止10统计
a.endswith() 判断是不是以什么结尾的,如果是返回true,也可以加起始位置和结束位置,判断子序列是不是以什么结尾的
a.expandtabs()将tabs转换成空格,默认情况下将一个tabs转换成8个空格
  >>> a = 'a	lex’ 
  >>> a.expandtabs() 
  'a lex’ 
a.find('e',0,5) 找某个字符,返回这个字符的位置,可以设置找的起始位置和结束位置,如果找的字符不存在,返回-1
a.index() 也是找,和find的不同,找不到的时候直接抛出异常报错
a.format() 就是做字符串格式化的,内部调用__format__()
    两种写法:
    >>> a = "alex {0} as {1}” 
    >>> a.format('sb','eric’) 
    'alex sb as eric’ 
    >>> a ="alex {name} as {id}”
    >>> a.format(name='sb',id = 'eric’) 
    'alex sb as eric’ 
isalnum()是否是字母或者数字
isalpha()是否是字母
isdecimal()是否是10进制小数
isdigit()是否是数字
isidentifier()是否是关键字
islower()是否全部是小写
isnumeric()是否是数字
isprintable()是否可以打印,忽略
isspace()是否是空格
istitle()是否是标题,判断每一个字母首字母都是大写
isupper()是否全部是大写
‘’,join(list) 拼接
ljust() 和center功能相似,左对齐
rjust()
lower() 全部变成小写
lstrip()
maketrans()做一个对应表,和translate()结合使用,来做替换
translate()
    >>> intab = 'abcde’ 
    >>> outtab = ‘12345'
    >>>trantab = ''.maketrans(intab,outtab)   
    >>> trantab
     {97: 49, 98: 50, 99: 51, 100: 52, 101: 53}
    >>> l_str = 'this is a ,that is bc'
    >>> l_str.translate(trantab)
    'this is 1 ,th1t is 23'
partition() 做分割,把字符串分割成3部分
    >>> a = 'alexissb’ 
    >>> a.partition('is’)
    ('alex', 'is', 'sb’)
replace()替换
    replace(‘old’,’new’,个数)
rfind()从右向左找
split()
splitlines()根据行分割,没有它用split(‘
')也能实现
strip()
startswith()以什么开头
swapcase() 大小写转换
title() 将字符串中所有的字母的首字母大写
upper() 大写
zfill()
View Code

python2中使用os.system执行系统命令

>>> a = os.system('df -lh')
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk1 465Gi 286Gi 179Gi 62% 1341378 4293625901 0% /
>>> a
0

这里看到变量a的值是0,却不是命令执行的结果

使用os.popen()可以获得命令执行结果

>>> b = os.popen('df -lh')
>>> b.read()
'Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk1 465Gi 286Gi 179Gi 62% 1341376 4293625903 0% /
'

os.popen()相当于打开了一个临时文件存储系统命令执行的结果,所以要用b.read()读取
View Code

python2中还可以使用subprocess模块执行操作系统命令。

>>> subprocess.call(['df','-lh'])
Filesystem     Size   Used  Avail Capacity  iused    ifree %iused  Mounted on
/dev/disk1    465Gi  209Gi  256Gi    45% 54730457 67109157   45%   /
/dev/disk2s1  741Mi  557Mi  184Mi    76%   142675    47036   75%   /Volumes/Parallels Desktop 11
/dev/disk3s2  786Mi  669Mi  117Mi    86%   171353    29853   85%   /Volumes/ParallelsDesktop 11.1.3-32521
0
这里给call方法,传“df -lh”这种多个参数时,用的是传入列表的形式['df','-lh’],python解释器在处理subprocess.call()方法时,将传进来的列表参数经过处理,最终转换成shell中的df -lh,那么如果我就不想传列表,怎么办呢。如下方法
 >>> subprocess.call('df -lh',shell=True)
Filesystem     Size   Used  Avail Capacity  iused    ifree %iused  Mounted on
/dev/disk1    465Gi  209Gi  256Gi    45% 54730303 67109311   45%   /
/dev/disk2s1  741Mi  557Mi  184Mi    76%   142675    47036   75%   /Volumes/Parallels Desktop 11
/dev/disk3s2  786Mi  669Mi  117Mi    86%   171353    29853   85%   /Volumes/ParallelsDesktop 11.1.3-32521
0
这里 subprocess.call('df -lh',shell=True)就明确告诉python解释器,你不用给我转了,就使用shell来进行执行。
 
怎么实现获取执行结果呢?
>>> a = subprocess.call("df -lh",shell=True)
Filesystem     Size   Used  Avail Capacity  iused    ifree %iused  Mounted on
/dev/disk1    465Gi  209Gi  256Gi    45% 54730696 67108918   45%   /
/dev/disk2s1  741Mi  557Mi  184Mi    76%   142675    47036   75%   /Volumes/Parallels Desktop 11
/dev/disk3s2  786Mi  669Mi  117Mi    86%   171353    29853   85%   /Volumes/ParallelsDesktop 11.1.3-32521
>>> a
0
这里我们看到0为执行命令的返回状态。
 
如果想存下来,不能用call,要用Popen方法。
>>> a = subprocess.Popen('df -lh',shell=True)
>>> Filesystem     Size   Used  Avail Capacity  iused    ifree %iused  Mounted on
/dev/disk1    465Gi  209Gi  256Gi    45% 54852887 66986727   45%   /
/dev/disk2s1  741Mi  557Mi  184Mi    76%   142675    47036   75%   /Volumes/Parallels Desktop 11
/dev/disk3s2  786Mi  669Mi  117Mi    86%   171353    29853   85%   /Volumes/ParallelsDesktop 11.1.3-32521
 
>>> a
<subprocess.Popen object at 0x10881cb50>
看到这个和os模块的Popen方法得到的一样。那么我们试下a.read()
发现没有read()方法。在试下a.stdout.read()
同样没有a.stdout.read()方法。
那么分析下python运行Popen方法的执行过程。
Popen()方法执行里面的shell命令,其实是python又开启了一个子进程,子进程运行的结果要想返回给Popen()方法,需要使用管道,写法如下:
>> a = subprocess.Popen('df -lh',shell=True,stdout=subprocess.PIPE)
>>> a.stdout.read()
'Filesystem     Size   Used  Avail Capacity  iused    ifree %iused  Mounted on
/dev/disk1    465Gi  209Gi  255Gi    46% 54971946 66867668   45%   /
/dev/disk2s1  741Mi  557Mi  184Mi    76%   142675    47036   75%   /Volumes/Parallels Desktop 11
/dev/disk3s2  786Mi  669Mi  117Mi    86%   171353    29853   85%   /Volumes/ParallelsDesktop 11.1.3-32521
’
总结:
使用subprocess模块,想获得命令执行结果。1.使用Popen方法 2.使用管道3.使用a.stdout.read()方法记住下面的例子即可:
 a = subprocess.Popen('df -lh',shell=True,stdout=subprocess.PIPE)
View Code

python3中使用os.system执行系统命令,和python2相同

python3中使用subprocess.Popen()方法和python2相同。但是在python3中subprocess模块中多了一个subprocess.run()方法。

记住:python3.5就不用Popen,想得到命令的执行结果用subporcess.run()方法即可,run方法在2.7中是没有的

>>> B = subprocess.run('df -lh',shell=True,stdout=subprocess.PIPE)
>>> type(B)
<class 'subprocess.CompletedProcess'>
>>> B
CompletedProcess(args='df -lh', returncode=0, stdout=b'Filesystem   Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk1  465Gi  286Gi  179Gi    62% 1341486 4293625793    0%   /
')
>>> B.stdout.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'bytes' object has no attribute 'read'
>>> B.stdout
b'Filesystem   Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk1  465Gi  286Gi  179Gi    62% 1341486 4293625793    0%   /
'
>>> B.stdout.decode()
'Filesystem   Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk1  465Gi  286Gi  179Gi    62% 1341486 4293625793    0%   /
'
View Code
 
原文地址:https://www.cnblogs.com/zhming26/p/6425158.html