Python的系统管理_06_pytho

发布时间:2019-06-26 09:48:38编辑:auto阅读(1407)

    import subprocess

    res =subprocess.Popen(['uname','-sv'],stdout=subprocess.PIPE)

    uname = res.stdout.read().strip()


    find()index()::

    uname,index('Linux')

    uname.find('Linux')

    uname.index('asdfasdf')

    uname.find('adfasdfasd')


    #字符串切分

    smp_index = uname.index ('SMP')

    uname[smp_index:]

    uname[:smp_index]


    startswith()endswith()

    这两个方法可以帮助你判断字符串时是否以某一特定子串开始,或是以某一特定子串结束。

    some_string = "RaymondLuxury-Yacht"

    some_string.startswith("Raymond")

    True

    some_string.startswith("sdfasdf")

    False

    some_string.endswith("Luxury-Yacht")

    True

    some_string.endswith("Raymond")

    False


    lstrip(),rstrip(),strip()

    分别用来删除前导空白,结尾空白,和前后空白的方法。

    spacious_string= "\n\t SomeNon-Spacious Text \n \t \r"

    spacious_string.lstrip()

    spacious_string.rstrip()

    spacious_string.strip()


    另外可以输入参数删除任何内容。

    xml_tag="<some_tag>"

    xml_tag.lstrip("<")

    xml_tag.lstrip(">")

    xml_tag.rstrip("<")

    xml_tag.rstrip(">")

    嵌套使用,xml_tag.strip("<").strip(">")

    xml_tag.strip("<>")

    .strip 是删除<>的任意组好即><也将被删除

    foo_str ="<foooooooooooo>blah<foo>"

    foo_str.strip("<foo>")


    upper()方法和lower()方法

    对两个字符串进行比较,并且不考虑字符大写或是小写。

    upper()将返回字符串的大写。

    lower()将返回字符串的小写。


    根据某个指定的分隔符对一个字符串进行提取,split()方法可以完成类任务。

    comma_delim_string ="pos1,pos2,pos3"

    pipe_delim_string ="pipepos1|pipepos2|pipepos3"

    comma_delim_string.split(',')

    pipe_delim_string.split('|')


    split(',',1) ','为分割,之分割第一个遇到的''

    prosaic_string = "Isert your cleverlittle"

    prosaic_string.split()#这个默认是用空格来分隔的。

    prosaic_string.splitlines()#将会以行为分隔。


    join()

    some_list = ['one','two','three','four']

    ','.join(some_list)

    ','.join(str(i)for i in some_list)#解决输入为数字的错误,显示转换为字符。


    replace()替换字符串。

    replacable_string = "trancendtalhibernational nation"

    replacable_string.replace("nation","natty")


    import re#引入正则表达式的使用。

    re_string = r"{{(.*?)}}"

    some_string = "this is a string with`words` em `bbq`and {jjfa} "

    for match inre.findall(re_string,some_string):

    print"MATCH->" ,match


    表达式转换为对象的编译:

    import re

    re_obj = re.compile(r"{{(.*?)}}")

    some_string = "this is a string with`words` em `bbq`and {jjfa} "

    for match in re_obj.findall(some_string):

    print"MATCH->" ,match

    这种方式速度会很快,

    例:

    采用timeit 可以测试出一段代码的运行时间。

    1截取一段50万行的文本

    tail -n 500000 Pa1.log >> 50Wlog.log

    编写代码:re_loop_nocompile.py

    ipython下运行

    import re_loop_nocompile

    timeit -n 5 re_loop_nocompile.run_re()

    显示最好的运行效率。1.42 最佳的。


    使用linuxtime工具对相同代码的测试结果:

    测试命令为:

    time python re_loop_nocompile.py


    编译后的代码性能:

    re_loop_compile.py

    效率提升至543ms


    所以应当在运行正则中使用编译的方式。


    在运行正则表达式中r的作用如下例:

    import re

    raw_pattern = r'\b[a-z]+\b'

    non_raw_pattern = '\b[a-z]+\b'

    some_string = 'a few little words'

    re.findall (raw_pattern , some_string)

    re.findall (non_raw_pattern,some_string)


    findall()使用:

    import re

    re_obj = re.compile(r'\bt.*?e\b')

    re_obj.findall("time tame tune tinttire")


    处理文件:

    infile =open("50Wlog.log","r")

    print infile.read()

    "r"读模式,该值为默认值

    "w"写模式

    "a"附加模式

    写入文件:

    outputfile =open("foo_out.txt","w")

    outputfile.write("This is \n Some \nRandom \n Output Text \n")

    outputfile.close()


    "try/finally"代码块

    try:

    f=open()

    f.write()

    finally:

    f.close()

    with 关键词:

    from __future__ import with_statment

    with open('writeable.txt','w')as f:

    f.writer('thisa writeable file \n')

    f

    f.write("this won't work")


    urllib模块,可以提供对网络中文件对象的访问。

    import urllib

    url_file = urllib.urlopen("http://192.168.112.96/index.html")

    urllib_docs = url_file.read()

    print urllib_docs


    使用ElementTree开始解析XML文件,只须简单的加载和使用parse()对文件进行处理:

    from xml.etree import ElementTree as ET

    tcusers = ET.parse("Tomcat.xml")

    tcusers

    xml 以文件名标签开始,并以文件名标签截至。

    这里将ElementTree as ET即为前面的对象。

    first_user=tcusers.find('/user')

    first_user

    first_user.attrib

    first_user.get('name')

    first_user.get('roles')

    first_user.get('foo')

    first_user.tag

    first_user.text

关键字