Category: Python

  • python map函数对list转换

    results = [‘1’, ‘2’, ‘3’]
    变为
    results = [1, 2, 3]

    使用

    results = map(int, results)

    反过来转换使用

    results = map(str, results)
  • python print颜色

    定义一个class

    class bcolors:
        HEADER = '\033[95m'           # 粉色
        OKBLUE = '\033[94m'           # 蓝色
        OKGREEN = '\033[92m'          # 绿色
        WARNING = '\033[93m'          # 黄色
        FAIL = '\033[91m'             # 红色
        BOLD = '\033[1m'              # 粗体
        ENDC = '\033[0m'              # 结束
    

    不管显示哪种颜色,最后都需要用end结束,不然会一直渲染。

    print bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC

    参考:
    http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python

    其他颜色可以参考

    #!/usr/bin/perl
    print "0\t\033[0m coloured! \033[m\n";
    print "1\t\033[1m coloured! \033[m\n";
    print "4\t\033[4m coloured! \033[m\n";
    print "7\t\033[7m coloured! \033[m\n";
    print "31\t\033[31m coloured! \033[m\n";
    print "32\t\033[32m coloured! \033[m\n";
    print "33\t\033[33m coloured! \033[m\n";
    print "34\t\033[34m coloured! \033[m\n";
    print "35\t\033[35m coloured! \033[m\n";
    print "36\t\033[36m coloured! \033[m\n";
    print "37\t\033[37m coloured! \033[m\n";
    print "38\t\033[38m coloured! \033[m\n";
    print "39\t\033[39m coloured! \033[m\n";
    print "40\t\033[40m coloured! \033[m\n";
    print "41\t\033[41m coloured! \033[m\n";
    print "42\t\033[42m coloured! \033[m\n";
    print "43\t\033[43m coloured! \033[m\n";
    print "44\t\033[44m coloured! \033[m\n";
    print "45\t\033[45m coloured! \033[m\n";
    print "46\t\033[46m coloured! \033[m\n";
    print "47\t\033[47m coloured! \033[m\n";
    print "90\t\033[90m coloured! \033[m\n";
    print "91\t\033[91m coloured! \033[m\n";
    print "92\t\033[92m coloured! \033[m\n";
    print "93\t\033[93m coloured! \033[m\n";
    print "94\t\033[94m coloured! \033[m\n";
    print "95\t\033[95m coloured! \033[m\n";
    print "96\t\033[96m coloured! \033[m\n";
    print "97\t\033[97m coloured! \033[m\n";
    print "98\t\033[98m coloured! \033[m\n";
    print "99\t\033[99m coloured! \033[m\n";
    print "100\t\033[100m coloured! \033[m\n";
    print "101\t\033[101m coloured! \033[m\n";
    print "102\t\033[102m coloured! \033[m\n";
    print "103\t\033[103m coloured! \033[m\n";
    print "104\t\033[104m coloured! \033[m\n";
    print "105\t\033[105m coloured! \033[m\n";
    print "106\t\033[106m coloured! \033[m\n";
    print "107\t\033[107m coloured! \033[m\n";
    print "108\t\033[108m coloured! \033[m\n";
    
  • python遍历dictionary

    如代码

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    dict={"a":"apple","b":"banana","o":"orange"}
    print "##########dict######################"
    for i in dict:
        print "dict[%s]=" % i,dict[i]
    print "###########items#####################"
    for (k,v) in dict.items():
        print "dict[%s]=" % k,v
    print "###########iteritems#################"
    for k,v in dict.iteritems():
        print "dict[%s]=" % k,v
    print "###########iterkeys,itervalues#######"
    for k,v in zip(dict.iterkeys(),dict.itervalues()):
        print "dict[%s]=" % k,v
    
  • python文件读写

    python写文件

    object_id_list=[1, 3, 88, 99]
    
    f=open('mylist', "w”)
    
    for id in object_id_list:
    
        f.writelines(str(id))
    
    f.close()   #只有输入这一句之后才会真正写入到文件中

    cat mylist

    有换行的时候

    138899%   # 最后有一个%表示没有换行
    >>> object_id_list=[1, 3, 88, 99]
    >>> f=open('mylist', "w")
    >>> for id in object_id_list:
    ...     f.writelines(str(id) + '\n')    # 换行
    ...
    >>> f.close()
    

    ➜  ~  cat mylist

    1
    3
    88
    99
    

    python读取json文件

    文件格式如下

    {
        "object_id": 430,
        "type": 23
    }
    
    object_id_read = open(object_id_file, "r")
    object_id_file_json = ''
    for line in object_id_read:
        object_id_file_json = object_id_file_json + line.strip('\n')
    data = json.loads(object_id_file_json)
    object_id = data['object_id']
    

    f.write 和 f.writelines有什么区别?

    f.read()
    f.readline()
    f.readlines()

  • python any判断一个对象是否为空

    any判断dict,list,tuple是不是为空

    >>> eth = {“eth0″:”192.168.1.1”}

    >>> any(eth)

    True

    >>> eth = {}

    >>> any(eth)

    False

  • python获取某目录下的所有文件和子目录-os.listdir

    有时候需要获取某目录下的所有文件和子目录,从stackoverflow搜到了一段代码,需要时可以使用。

    #!/usr/bin/python
    
    import os, sys
    
    # Open a file
    path = "/var/www/html/"
    dirs = os.listdir(path)
    
    # This would print all the files and directories
    for file in dirs:
       print file