1、编译python遇到下面的编码问题:
    SyntaxError: Non-ASCII character '\xe9' in file E:\projects\learn.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:

 import sys
print 'test module'
print 'before import test2', sys.modules.keys()
import test2
print 'after import test2', sys.modules.keys()
if __name__ == 'main':
import test

test2.py

 import sys
print 'test2 module'
print 'before import test', sys.modules.keys()
import test
print 'after import test', sys.modules.keys()

运行结果:

 test module
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']
test2 module
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']
test module
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']
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']
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']
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']
[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对象。具体例子如下:

 >>> t1 = ('this string', 42, [1, 2, 3])
>>> import pickle
>>> p1 = pickle.dumps(t1)
>>> p1
"(S'this string'\np0\nI42\n(lp1\nI1\naI2\naI3\natp2\n."
>>> t2 = pickle.loads(p1)
>>> t2
('this string', 42, [1, 2, 3])
>>>

26、 自定义迭代器

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

 class Iter(object):
def __init__(self, owner, start, stop):
self.owner = owner
self.value = start -
self.stop = stop
def next(self):
if self.value == self.stop:
raise StopIteration
self.value +=
return self.value ** class Squares(object): def __init__(self, start, stop):
self.start = start
self.stop = stop
def __iter__(self):
return Iter(self, self.start, self.stop) x = Squares(, )
for i in x:
for j in x:
print i, ' ', j

27、内置函数locals和globals

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

28、文本操作
将制表符转换为空格:string.expandtabs
29、写操作会屏蔽外部命名空间的搜索,只会搜索当前命名空间。命名空间的搜索是在编译器进行的。
30、print在windows控制台输出需要设置gbk编码格式:
      print s.encode('gbk')

python笔记(持续更新)的更多相关文章

  1. BLE资料应用笔记 -- 持续更新

    BLE资料应用笔记 -- 持续更新 BLE 应用笔记 小书匠 简而言之,蓝牙无处不在,易于使用,低耗能和低使用成本.'让我们'更深入地探索这些方面吧. 蓝牙无处不在-,您可以在几乎每一台电话.笔记本电 ...

  2. Python奇技淫巧 - 持续更新中....

    Python奇技淫巧 人生苦短,我用Python: 编程界这绝对不是一句空话,尤其是对于使用过多个语言进行工作的同学们来说,用Python的时间越长,越有一种我早干嘛去了的想法,没事,啥时候用Pyth ...

  3. [读书]10g/11g编程艺术深入体现结构学习笔记(持续更新...)

    持续更新...) 第8章 1.在过程性循环中提交更新容易产生ora-01555:snapshot too old错误.P257 (这种情况我觉得应该是在高并发的情况下才会产生) 假设的一个场景是系统一 ...

  4. react-native-storage 使用笔记 持续更新

    React-native-storage是在AsyncStorage之上封装的一个缓存操作插件库,刚开始接触这个也遇到了一些问题,在这里简单记录总结一下,碰到了就记下来,持续更新吧 1.安卓下stor ...

  5. python内置模块笔记(持续更新)

    常用函数name = '{wh}my \t name is {name},age is {age}.' print(name.capitalize()) # 字符串的开头字母大写 print(name ...

  6. 数据分析之Pandas和Numpy学习笔记(持续更新)<1>

    pandas and numpy notebook        最近工作交接,整理电脑资料时看到了之前的基于Jupyter学习数据分析相关模块学习笔记.想着拿出来分享一下,可是Jupyter导出来h ...

  7. BLE资料应用笔记 -- 持续更新(转载)

    简而言之,蓝牙无处不在,易于使用,低耗能和低使用成本.’让我们’更深入地探索这些方面吧. 蓝牙无处不在—,您可以在几乎每一台电话.笔记本电脑 .台式电脑和平板电脑中找到蓝牙.因此,您可以便利地连接键盘 ...

  8. Semantic ui 学习笔记 持续更新

    这个semantic 更新版本好快~ 首先是代码的标识<code></code> 具体样式就是红框这样的 圈起来代码感觉不错 不过要在semantic.css里在加上如下样式~ ...

  9. Git学习笔记(持续更新)

    1.强制同步为远程的代码 远程仓库回退了commit的情况下(第2条描述之情况),强制同步远程的代码到本地 #更新远程最新的所有代码,但是不merge或者rebase git fetch --all ...

  10. web前端开发随手笔记 - 持续更新

    本文仅为个人常用代码整理,供自己日常查阅 html 浏览器内核 <!--[if IE]><![endif]--> <!--[if IE 6]><![endif ...

随机推荐

  1. Database Replay和Consolidated Database replay

    简介 在数据库的迁移和升级场景中,我们经常会遇到一个问题:在做压力测试时,如何模拟真实的业务压力,解决这个问题的方法有很多,比如:应用方开发模拟程序或者使用压力测试工具模拟,如load runner, ...

  2. 关于面试题 Array.indexof() 方法的实现及思考

    这是我在面试大公司时碰到的一个笔试题,当时自己云里雾里的胡写了一番,回头也曾思考过,最终没实现也就不了了之了. 昨天看到有网友说面试中也碰到过这个问题,我就重新思考了这个问题的实现方法. 对于想进大公 ...

  3. 开始学nodejs——net模块

    net模块的组成部分 详见 http://nodejs.cn/api/net.html 下面整理出了整个net模块的知识结构,和各个事件.方法.属性的用法 net.Server类 net.Socket ...

  4. [.NET] 利用 async & await 的异步编程

    利用 async & await 的异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/5922573.html  目录 异步编程的简介 异 ...

  5. jQuery.template.js 简单使用

    之前看了一篇文章<我们为什么要尝试前后端分离>,深有同感,并有了下面的评论: 我最近也和前端同事在讨论这个问题,比如有时候前端写好页面给后端了,然后后端把这些页面拆分成很多的 views, ...

  6. scp报错 -bash: scp: command not found

    环境:RHEL6.5 使用scp命令报错: [root@oradb23 media]# scp /etc/hosts oradb24:/etc/ -bash: scp: command not fou ...

  7. C++ 11 多线程--线程管理

    说到多线程编程,那么就不得不提并行和并发,多线程是实现并发(并行)的一种手段.并行是指两个或多个独立的操作同时进行.注意这里是同时进行,区别于并发,在一个时间段内执行多个操作.在单核时代,多个线程是并 ...

  8. Java中用得比较顺手的事件监听

    第一次听说监听是三年前,做一个webGIS的项目,当时对Listener的印象就是个"监视器",监视着界面的一举一动,一有动静就触发对应的响应. 一.概述 通过对界面的某一或某些操 ...

  9. CSS入门常见的问题

    写在前面:本文简单介绍一下css的三大特性:层叠性.继承性.优先级.以及margin,padding,浮动,定位几个知识点.限于水平,不深入探讨,仅作为学习总结. 1,三特性 1)层叠性:同标签同权重 ...

  10. js闭包for循环总是只执行最后一个值得解决方法

    <style> li{ list-style: none;width:40px;height: 40px;text-align:center;line-height: 40px;curso ...