zoukankan      html  css  js  c++  java
  • python笔记(持续更新)

    1、编译python遇到下面的编码问题:
        SyntaxError: Non-ASCII character 'xe9' in file E:projectslearn.py on line 3, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
        解决方法:解决方法:源代码文件第一行添加:#coding:utf-8
    3、is表示引用是否是指向同一个对象,==表示引用指向对象的内容是否相同。
    4、globals函数可以查看变量的引用情况,getrefcount可以获得一个对象被引用的次数。
    5、struct.calcsize():用来计算特定格式的输出的大小,是几个字节
    6、inspect模块功能:

    (1).对是否是模块,框架,函数等进行类型检查。

    (2).获取源码

    (3).获取类或函数的参数的信息

    (4).解析堆栈

    7、python标准库:http://python.usyiyi.cn/python_278/library/index.html

    8、对于处理非ASCII字符的字符串,最好在输入时转换为unicode编码,在输出的时候使用对应的编码进行编码后再输出。
    9、可以将zip文件加入sys.path,然后可以通过import导入zip文件中的.py文件模块。读取zip文件可以用zipfile模块。直接处理zip文件字符串,可以直接用cStringIO中的StringIO模块,而不用先将字符串存到一个临时的zip文件中,再进行处理。StringIO可以看做是一个放在内存中的文件对象,适合于文件的操作都可以用在StringIO模块上。
    10、可以用tarfile模块将一个目录树归档到一个压缩的tar文件。
    11、判断当前系统:sys.platform。
    12、fnmath可以用来检测文件名匹配模式,os.walk可以用来遍历目录。
    13、xlwt、xlrd和xlutils.copy用来处理excel。
    14、当你觉得直接改变某列表而不是某列表时,列表推导常常是最好的方法。例如:假设需要将某列表L中的大于100的元素设置为100,最好的方法如下:
    L[ : ] = [min(x, 100) for x in L]
    此时的L并没有重新绑定一个新的列表,而是修改了原来列表的内容。
    15、把列表推导的[]改成()就是生成器表达式了。生成器表达式最好的一点就是不用一次性将所有数据加载如内存种。
    16、遍历列表并获得索引,最好用enumerate包装下。
    17、创建二维列表应该用列表推导,而不是用*,*只会复制引用。
    multilist = [[0 for col in range(5)] for row in range(3)]
    multilist2 = [[0] * 5] * 3
    虽然上面这个很简洁,不过会出现共享引用问题,即multilist2[0] == multilist2[1]
    18、给字典添加一个条目,d.setdefault(word, []).append(pagenumber)。
    19、itertools模块主要用来做产生器的,可以使数据不用一次性加载进入内存。
    20、random.choice随机获取列表中的元素。
    21、bisect二分查找。
    23、greenlet用协程实现并发:http://greenlet.readthedocs.org/en/latest/

    24、 循环import模块会怎样?

        python中循环导入不会怎么样,因为每个模块被import的时候只会执行一次,并且该模块的引用会存放在sys.modules中,后面如果再import该模块时,虚拟机会查看sys.modules是否存在该模块,如果存在则不导入。看看下面一个例子就一目了然了:

        test.py:

    1 import sys
    2 print 'test module'
    3 print 'before import test2', sys.modules.keys()
    4 import test2
    5 print 'after import test2', sys.modules.keys()
    6 if __name__ == 'main':
    7     import test

        test2.py

    1 import sys
    2 print 'test2 module'
    3 print 'before import test', sys.modules.keys()
    4 import test
    5 print 'after import test', sys.modules.keys()

        运行结果:

     1 test module
     2 before import test2 ['copy_reg', 'sre_compile', 'locale', '_sre', 'functools', 'encodings', 'site', '__builtin__', 'sysconfig', 'operator', '__main__', 'types', 'encodings.encodings', 'encodings.gbk', 'abc', '_weakrefset', 'encodings._codecs_cn', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'ntpath', '_codecs', 'encodings._multibytecodec', 'nt', '_warnings', 'genericpath', 'stat', 'zipimport', 'encodings.__builtin__', 'warnings', 'UserDict', '_multibytecodec', 'sys', 'codecs', 'os.path', '_functools', '_codecs_cn', '_locale', 'signal', 'traceback', 'linecache', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
     3 test2 module
     4 before import test ['test2', 'copy_reg', 'sre_compile', 'locale', '_sre', 'functools', 'encodings', 'site', '__builtin__', 'sysconfig', 'operator', '__main__', 'types', 'encodings.encodings', 'encodings.gbk', 'abc', '_weakrefset', 'encodings._codecs_cn', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'ntpath', '_codecs', 'encodings._multibytecodec', 'nt', '_warnings', 'genericpath', 'stat', 'zipimport', 'encodings.__builtin__', 'warnings', 'UserDict', '_multibytecodec', 'sys', 'codecs', 'os.path', '_functools', '_codecs_cn', '_locale', 'signal', 'traceback', 'linecache', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
     5 test module
     6 before import test2 ['test2', 'copy_reg', 'sre_compile', 'locale', '_sre', 'functools', 'encodings', 'site', '__builtin__', 'sysconfig', 'operator', '__main__', 'types', 'encodings.encodings', 'encodings.gbk', 'abc', '_weakrefset', 'encodings._codecs_cn', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'ntpath', '_codecs', 'test', 'encodings._multibytecodec', 'nt', '_warnings', 'genericpath', 'stat', 'zipimport', 'encodings.__builtin__', 'warnings', 'UserDict', '_multibytecodec', 'sys', 'codecs', 'os.path', '_functools', '_codecs_cn', '_locale', 'signal', 'traceback', 'linecache', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
     7 after import test2 ['test2', 'copy_reg', 'sre_compile', 'locale', '_sre', 'functools', 'encodings', 'site', '__builtin__', 'sysconfig', 'operator', '__main__', 'types', 'encodings.encodings', 'encodings.gbk', 'abc', '_weakrefset', 'encodings._codecs_cn', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'ntpath', '_codecs', 'test', 'encodings._multibytecodec', 'nt', '_warnings', 'genericpath', 'stat', 'zipimport', 'encodings.__builtin__', 'warnings', 'UserDict', '_multibytecodec', 'sys', 'codecs', 'os.path', '_functools', '_codecs_cn', '_locale', 'signal', 'traceback', 'linecache', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
     8 after import test ['test2', 'copy_reg', 'sre_compile', 'locale', '_sre', 'functools', 'encodings', 'site', '__builtin__', 'sysconfig', 'operator', '__main__', 'types', 'encodings.encodings', 'encodings.gbk', 'abc', '_weakrefset', 'encodings._codecs_cn', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'ntpath', '_codecs', 'test', 'encodings._multibytecodec', 'nt', '_warnings', 'genericpath', 'stat', 'zipimport', 'encodings.__builtin__', 'warnings', 'UserDict', '_multibytecodec', 'sys', 'codecs', 'os.path', '_functools', '_codecs_cn', '_locale', 'signal', 'traceback', 'linecache', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
     9 after import test2 ['test2', 'copy_reg', 'sre_compile', 'locale', '_sre', 'functools', 'encodings', 'site', '__builtin__', 'sysconfig', 'operator', '__main__', 'types', 'encodings.encodings', 'encodings.gbk', 'abc', '_weakrefset', 'encodings._codecs_cn', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'ntpath', '_codecs', 'test', 'encodings._multibytecodec', 'nt', '_warnings', 'genericpath', 'stat', 'zipimport', 'encodings.__builtin__', 'warnings', 'UserDict', '_multibytecodec', 'sys', 'codecs', 'os.path', '_functools', '_codecs_cn', '_locale', 'signal', 'traceback', 'linecache', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
    10 [Finished in 0.2s]

        从执行结果来看,test先import test2,由于sys.modules中没有test2,所以执行test2,并将test2加入sys.modules中;在test2中,import test1,由于sys.modules中没有test1,所以执行test1,并将test1加入sys.modules中;执行到import test2时,由于此时sys.modules中存在了test2,所以不执行test2,等到test1执行完成后回到test2继续执行;test2继续执行完成后回到最先的test执行。

    25、pickle对象持久化

        pickle用法很简单,将一个python对象通过dumps序列化为字符串,如果通过loads将一个str转化为一个python对象。具体例子如下:

    1 >>> t1 = ('this string', 42, [1, 2, 3])
    2 >>> import pickle
    3 >>> p1 = pickle.dumps(t1)
    4 >>> p1
    5 "(S'this string'
    p0
    I42
    (lp1
    I1
    aI2
    aI3
    atp2
    ."
    6 >>> t2 = pickle.loads(p1)
    7 >>> t2
    8 ('this string', 42, [1, 2, 3])
    9 >>>

    26、 自定义迭代器

        在class中定义__iter__和next函数即可,具体如下:

     1 class Iter(object):
     2     def __init__(self, owner, start, stop):
     3         self.owner = owner
     4         self.value = start - 1
     5         self.stop = stop
     6     def next(self):
     7         if self.value == self.stop:
     8             raise StopIteration
     9         self.value += 1
    10         return self.value ** 2
    11 
    12 class Squares(object):
    13 
    14     def __init__(self, start, stop):
    15         self.start = start
    16         self.stop = stop
    17     def __iter__(self):
    18         return Iter(self, self.start, self.stop)
    19 
    20 x = Squares(1, 5)
    21 for i in x:
    22     for j in x:
    23         print i, ' ', j

     27、内置函数locals和globals

    主要是语句执行的上下文环境。

    28、文本操作
    将制表符转换为空格:string.expandtabs
    29、写操作会屏蔽外部命名空间的搜索,只会搜索当前命名空间。命名空间的搜索是在编译器进行的。
    30、print在windows控制台输出需要设置gbk编码格式:
          print s.encode('gbk')
  • 相关阅读:
    单步调试理解webpack里通过require加载nodejs原生模块实现原理
    webpack打包过程如何调试?
    django-数据库之连接数据库
    django-模板之include标签(十五)
    django-模板之静态文件加载(十四)
    django-模板之过滤器Add(十三)
    django-模板之with标签(十二)
    django-模板之for empty(十一)
    django-模板之if语句(九)
    django-模板之自动转义autoescape(八)
  • 原文地址:https://www.cnblogs.com/chengxuyuancc/p/4856342.html
Copyright © 2011-2022 走看看