• python使用platform模块获取系统环境并去除换行符


    近来在porting一个网站,企图拿到这个网站的数据来做分析。为了支持多系统环境的正常运行。需要知道当前系统环境的是什么OS?

    1.python内置platform库。可以很方便得到当前系统环境时什么OS系统。

    import platform
    print platform.system() #获取操作系统环境
    print platform.platform() #获取操作系统名称及版本号
    print platform.version() #获取操作系统版本号
    print platform.architecture()#获取操作系统的位数
    print platform.machine()#计算机类型
    print platform.node() #计算机的网络名称
    print platform.processor() #计算机处理器信息
    help(platform)#太多了不一一个列举,求帮助
    
    >>>
    Windows
    Windows-7-6.1.7601-SP1
    6.1.7601
    ('32bit', 'WindowsPE')
    x86
    szdliunx
    x86 Family 6 Model 58 Stepping 9, GenuineIntel

    2.去除换行符。

    不知道大家对换行符有多少了解?先简单介绍下,不同的操作系统,换行符的定义。

    Unix/Linux系统里,每行结尾只有“<换行>”,即“ ”;

    Windows系统里面,每行结尾是“<回车><换行>”,即“ ”;

    Mac系统里,每行的结尾是“"<回车>”,即“ ”.

    对于换行这个动作,unix/Linux环境下一般只有一个0x0A表示换行(" "),windows下一般都是0x0D和0x0A两个字符(" "),苹果机(MAC OS系统)则采用回车符CR 0x0D表示下一行( ).

    3.最终的代码。

        if platform.system() == "Windows":
            f.write((data.get_text().strip('
    ')))
        elif platform.system() == "Linux":
            f.write((data.get_text().strip('
    ')))
        else:#for mac os
            f.write((data.get_text().strip('
    ')))

     

    
    
  • 相关阅读:
    识别IE11浏览器
    国庆过后老革命
    有些东西再忙也要做
    云计算
    SVN下Update出现代码文件删除状态问题
    如何避免历史回退到登录页面
    CodeSmith连Oracle
    NHibernate直接执行SQL进行插入
    nhibernate实体类主键ID赋值问题
    NHibernate不支持复杂的linq,就一定要用DataTable这么低级吗
  • 原文地址:https://www.cnblogs.com/nx520zj/p/5864013.html
Copyright © 2020-2023  润新知