zoukankan      html  css  js  c++  java
  • Python 笔记

    1. os.linesep是干什么的?
    os.linesep字符串给出当前平台使用的行终止符。例如,Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。

    2.解释器行为吧,被解释器引用计数了
    int会 float不会
    >>> a=5
    >>> b=5
    >>> a is b
    True

    >>> a=1.1
    >>> b=1.1
    >>> a is b
    False

    3.使用随机数
    >>> import random
    >>> print random.uniform(1,10)
    3.65089367909

    4.有的IDE可以打断点,PYDEV就可以,利用pdb可以单步调试

    5. 查询是否为关键字 >>> import keyword >>> keyword.iskeyword('try')

    6.scapy ——网络包构建分析框架,可编程的wireshark,有兴趣的google “Silver Needle in the Skype”

    7.python的安装
    # rm -f /usr/bin/python
    # ln -s ~/Desktop/python /usr/bin/python

    8.将一个二维列表平坦化
    >>> list = [[1, 3, 5], [2, 4]]
    >>>print [flatten for inner in list for flatten in inner]
    [1, 3, 5, 2, 4]

    9.查询某个导入类的方法
    dir(email.generator)
    help(email.generator.Generator)
    print smtplib.encode_base64('codemo')

    10.打印当前时间
    print (datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))

    11.python3.0 中去掉了raw_input 函数,改用input。
    12.python3.0 已经不支持了
    apply(self.func,self.args)
    改为
    self.func(*self.args)

    13.py3的encode与decode
    >>> print ('中国'.encode('utf-8'))
    b'\xe4\xb8\xad\xe5\x9b\xbd'
    >>> print (b'\xe4\xb8\xad\xe5\x9b\xbd'.decode('utf-8'))
    中国
    >>>print (b'\u8bf7\u8f93\u5165\u95ee\u9898\u7b54\u6848'.decode('unicode-escape'))
    请输入问题答案

    subject='\u8bf7\u8f93\u5165\u95ee\u9898\u7b54\u6848'
    subject = bytes(subject,'gb2312').decode('unicode-escape')
    >>>print subject

    解决中文通用办法
    froms=froms.encode('raw_unicode_escape', 'ignore').decode('GB18030', 'ignore')

    14.urlencode
    >>> import urllib
    >>> urllib.unquote("myemail%40gmail.com") #py2.7
    >>>urllib.parse.unquote("myemail%40gmail.com") #py3.0

    15.把一个py文件编译为pyc文件了。
    import py_compile
    py_compile.compile(r'H:\game\test.py') #windows环境下
    py_compile.compile('wbm_sina.py') # Linux环境

    16.时间格式化
    >>> date='Wed, 11 Apr 2012 09:37:05 +0800'
    >>> dd=datetime.datetime.strptime(date,'%a, %d %b %Y %H:%M:%S %z')
    >>> dd.strftime('%Y-%m-%d %H:%M:%S')

    17.html解码
    #html和url解码
    def HTMLDecode(html):
    return html.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('&quot;', '\"').replace("&#39;", '\'')

    18.文件以指定格式打开
    fp = open(msgfile,encoding='gb18030')

    19.替换打开文件的某个内容,必须用re

    open('2.txt', 'w').write(re.sub(r'world', 'python', open('1.txt').read()))

    20.问下python怎么将10进制数字转成ascii字符
    已知'&#64;' 怎么求出 对应字符 @
    ss=chr(64) #ss为@

    21.字符串与字典、列表的转换
    >>> attname=r'[{"id":"b2a5de4dd124e648318ba89ea82cf47a7adc5a9f_172425515","name":"360.jpg","type":"image/jpeg","size":"50046"},{"id":"978ae598cc78b39aa2439c38fba6b655fc8811e1_172425515","name":"%E6%9C%AA%E5%91%BD%E5%90%8D.jpg","type":"image/jpeg","size":"23242"},{"id":"24ae02599c638297edf17979600ad638a7e9b6d9_172425515","name":"%E4%B8%B4%E4%BE%A6%E4%BE%A6%E6%8E%A7%E7%B3%BB%E7%BB%9F%E5%90%8E%E5%8F%B0%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1.doc","type":"application/msword","size":"242432"}]'
    >>> a2=eval(attname)
    >>> a2[0]
    {'size': '50046', 'type': 'image/jpeg', 'id': 'b2a5de4dd124e648318ba89ea82cf47a7adc5a9f_172425515', 'name': '360.jpg'}
    >>> a2dict=eval(str((a2)[0]))
    >>> a2dict['name']
    '360.jpg'

  • 相关阅读:
    Hadoop集群(三) Hbase搭建
    Hadoop集群(二) HDFS搭建
    Hadoop集群(一) Zookeeper搭建
    Redis Cluster 添加/删除 完整折腾步骤
    Redis Cluster在线迁移
    Hadoop分布式HA的安装部署
    Describe the difference between repeater, bridge and router.
    what is the “handover” and "soft handover" in mobile communication system?
    The main roles of LTE eNodeB.
    The architecture of LTE network.
  • 原文地址:https://www.cnblogs.com/codemo/p/PythonTips.html
Copyright © 2011-2022 走看看