Python中的数据类型转换举例及脚本统计服务器内存实例

简介:

统计系统剩余的内存 

    In [1]: s1 = 'abc'

    In [2]: help(s1.startswith)

    

    Help on built-in function startswith:

    startswith(...)

    S.startswith(prefix[, start[, end]]) -> bool

    Return True if S starts with the specified prefix, False otherwise.

    With optional start, test S beginning at that position.

    With optional end, stop comparing S at that position.

    prefix can also be a tuple of strings to try.

    (END)

cat /proc/meminfo


    #!/usr/bin/python

    with open('/proc/meminfo') as fd:

        for line in fd:

            if line.startswith('MemTotal:'):

                total = line.split()[1]

                continue

            if line.startswith('MemFree:'):

                free = line.split()[1]

                break

    print "%.2f" % (int(free)/1024.0)+'M'


数据类型转换计算(计算mac地址)

10进制转换成16进制:

    In [9]: hex(10)

    Out[9]: '0xa'


16进制转换成10进制:

    In [8]: int('0xa',16)

    Out[8]: 10

    In [7]: int('aa',16)

    Out[7]: 170


纯数字的字符串转换成10进制:

    In [10]: int('10')

    Out[10]: 10


10进制转换成字符串:

    In [11]: str(10)

    Out[11]: '10'

举例:

原有服务器mac:02:42:0e:31:98:0b,写脚本生成下一个网卡的mac,自动加1。

    #!/usr/bin/python

    macaddr = '02:42:0e:31:98:0b'

    prefix_mac = macaddr[:-3]

    last_two = macaddr[-2:]

    plus_one = int(last_two,16)+1

    if plus_one in range(10):

        new_last_two = hex(plus_one)[2:]

        new_last_two = '0' + new_last_two

    else:

        new_last_two = hex(plus_one)[2:]

    if len(new_last_two) == 1:

        new_last_two = '0' +new_last_two

    new_mac = prefix_mac + ':' + new_last_two

    print new_mac.upper()

数据类型转换(列表与字典相互转换)

查看帮助join()

    Help on built-in function join:

    join(...)

    S.join(iterable) -> string

    Return a string which is the concatenation of the strings in the

    iterable.  The separator between elements is S.

    (END)


字符串转列表:list(string)

    In [36]: a = list('aaa')

    In [37]: type(a)

    Out[37]: list

    In [38]: a

    Out[38]: ['a', 'a', 'a']


列表转字符串:'''.join(list)

    In [38]: a

    Out[38]: ['a', 'a', 'a']

    In [39]:l = a

    In [18]: l

    Out[18]: ['a', 'a', 'a']

    In [19]: ''.join(l)

    Out[19]: 'aaa'

    In [20]: ','.join(l)

    Out[20]: 'a,a,a'

    In [21]: '.'.join(l)

    Out[21]: 'a.a.a'

    In [22]: a= 'a'

    In [23]: help(a.join)


字符串转元组:tuple(string)

    In [24]: s

    Out[24]: ['a', 'a', 'a']

    In [26]: tuple(s)

    Out[26]: ('a', 'a', 'a')


元组转字符串:''.join(tuple)

    In [54]: type(a)

    Out[54]: tuple

    In [55]: a = str(a)

    In [56]: a

    Out[56]: "('a', 'b', 'c', 111)"

    In [57]: type(a)

    Out[57]: str


字典转列表:

    In [28]: dic = {'a':1,'b':2}

    In [29]: dic

    Out[29]: {'a': 1, 'b': 2}

    In [30]: dic.items()

    Out[30]: [('a', 1), ('b', 2)]


列表转字典:

    In [31]: l1 = dic.items()

    In [32]: l1

    Out[32]: [('a', 1), ('b', 2)]

    In [33]: type(l1)

    Out[33]: list

    In [34]: dict(l1)

    Out[34]: {'a': 1, 'b': 2}




本文转自 枫叶云  51CTO博客,原文链接:http://blog.51cto.com/fengyunshan911/2056008
相关文章
|
7月前
|
存储 弹性计算 安全
阿里云渠道商:新手如何选择阿里云ECS实例?
阿里云ECS凭借弹性扩展、稳定可靠与安全防护,助力企业高效上云。本文系统解析实例规格选择关键因素:业务场景匹配、性能评估、成本优化、地域部署与扩展规划,结合计费模式与实际需求,提供科学选型建议,助您精准匹配资源,提升云上效能。(238字)
|
8月前
|
存储 弹性计算 网络协议
阿里云服务器ECS通用算力型u2a实例,CPU采用AMD EPYC处理器,睿频最高3.7 GHz
阿里云ECS通用算力型u2a实例搭载AMD EPYC处理器,睿频高达3.7GHz,基于CIPU架构,网络与存储突发带宽最高25Gbps,I/O性能强、延迟低。支持多种云盘及IPv4/IPv6,适用于中小型数据库、APP服务等场景,性价比高,官网价降低9%-22%,是中小企业上云优选。
957 0
|
7月前
|
弹性计算 定位技术 数据中心
阿里云服务器配置选择方法:付费类型、地域及CPU内存配置全解析
阿里云服务器怎么选?2025最新指南:就近选择地域,降低延迟;长期使用选包年包月,短期灵活选按量付费;企业选2核4G5M仅199元/年,个人选2核2G3M低至99元/年,高性价比爆款推荐,轻松上云。
845 11
|
7月前
|
弹性计算 人工智能 前端开发
在阿里云ECS上部署n8n自动化工作流:U2实例实战
本文介绍如何在阿里云ECS的u2i/u2a实例上部署开源工作流自动化平台n8n,利用Docker快速搭建并配置定时任务,实现如每日抓取MuleRun新AI Agent并推送通知等自动化流程。内容涵盖环境准备、安全组设置、实战案例与优化建议,助力高效构建低维护成本的自动化系统。
1844 5

推荐镜像

更多