Python Tips

发布时间:2019-10-14 09:22:07编辑:auto阅读(2445)

    # 豆瓣
    pip3 install psutil -i https://pypi.doubanio.com/simple/
    # 阿里云
    pip3 install psutil -i https://mirrors.aliyun.com/pypi/simple/
    • pip install 使用代理
    # 须先安装 PySocks 才能使用 SOCKS5 代理
    pip3 install PySocks
    pip3 install psutil --proxy socks5://192.168.0.172:10080
    • Python用print打印html文档时,若不打印协议首部,可能无法输出html文档。
    print('Content-type: text/html\r\n')
    • Python2.7 搭建简单http server,只能解析静态文件。
    python2.7  -m  SimpleHTTPServer 5678
    • Python3 搭建简单http server,只能解析静态文件。
    python3 -m http.server 5678
    • Python2.7 搭建能处理python脚本的http server。
    python2.7 -m CGIHTTPServer 5678
    • Python3 搭建能处理python脚本的http server。
    from http.server import HTTPServer, CGIHTTPRequestHandler
    port = 5678
    httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
    print("Starting simple_httpd on port: " + str(httpd.server_port))
    httpd.serve_forever()
    • Python 的三种数据类型字典、列表、元组,分别用花括号、中括号、小括号表示。如:
    字典:dic={'a':12, 'b':34}
    列表:li=[1, 2, 3, 3]
    集合:s = {1, 2, 3, 4}    # set是无序的无重复元素的列表
    元组:tup=(1, 2, 3, 4)    # 元组是不可更改的列表
    • Python 打印不换行

    (1)、通用方法

    import sys
    sys.stdout.write("no new line")

    (2)、Python2 print 不换行(加逗号)

    print 'no new line',

    (3)、Python3 print 不换行

    print('no new line', end='')
    • Python 2.x 在使用help函数时,对内置函数一定要加引号
    help(print)    # wrong
    help('print')  # right
    • Python 模块的一般安装方法:
    python setup.py install
    • Python 的全局变量若在函数内部被修改,会被编译器认为是局部变量,解决办法是在函数内用global声明这个变量。
    • Python打印异常信息。
    try:
        #do someting
    except:
        import traceback
        print(traceback.format_exc())
        traceback.print_exc()    # 约等于上句
    • python中获取python版本号的方法
    import platform
    print(platform.python_version())
    
    import sys
    print(sys.version)
    • 查看 Python 的搜索路径。
    >>> import sys
    >>> print sys.path

    任何情况下都只认 sys.path!参见:分别描述python2.x和python3.x import 包时的路径搜索顺序!

    当你导入一个模块,Python 解析器对模块位置的搜索顺序是:
    1、当前目录
    2、如果不在当前目录,Python 则搜索在 shell 变量 PYTHONPATH 下的每个目录。
    3、如果都找不到,Python会察看默认路径。UNIX下,默认路径一般为/usr/local/lib/python/。
    模块搜索路径存储在 system 模块的 sys.path 变量中。变量里包含当前目录,PYTHONPATH和由安装过程决定的默认目录。

    (1)、python 内建排序 HOW TO
    (2)、Python中sorted()方法的用法

    >>> import pip
    >>> from pprint import pprint
    >>> pprint(pip.pep425tags.get_supported())
    [('cp34', 'none', 'win_amd64'),
     ('py3', 'none', 'win_amd64'),
     ('cp34', 'none', 'any'),
     ('cp3', 'none', 'any'),
     ('cp33', 'none', 'any'),
     ('cp32', 'none', 'any'),
     ('cp31', 'none', 'any'),
     ('cp30', 'none', 'any'),
     ('py34', 'none', 'any'),
     ('py3', 'none', 'any'),
     ('py33', 'none', 'any'),
     ('py32', 'none', 'any'),
     ('py31', 'none', 'any'),
     ('py30', 'none', 'any')]
    >>> 33/22
    1.5
    >>> 3.3/2.2
    1.4999999999999998
    >>> 33/15
    2.2
    >>> 3.3/1.5
    2.1999999999999997
    >>> 2-1.1
    0.8999999999999999
    import warnings
    warnings.filterwarnings("ignore")
    class Tee(object):
        def __init__(self):
            self.terminal = sys.stdout
            self.log = open("log.log", "a")
            
        def __del__(self):
            sys.stdout = self.terminal
            self.log.close()
    
        def write(self, message):
            self.terminal.write(message)
            self.log.write(message)  
            self.log.flush()
            
    sys.stdout = Tee()
    
    print('HaHaHa')
    本文出自 walker snapshot

关键字