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. iOS可视化动态绘制连通图

    上篇博客<iOS可视化动态绘制八种排序过程>可视化了一下一些排序的过程,本篇博客就来聊聊图的东西.在之前的博客中详细的讲过图的相关内容,比如<图的物理存储结构与深搜.广搜>.当 ...

  2. Ubuntu 16.10 安装byzanz截取动态效果图工具

    1.了解byzanz截取动态效果图工具 byzanz能制作文件小,清晰的GIF动态效果图,不足就是,目前只能通过输入命令方式来录制. byzanz主要的参数选项有: -d, --duration=SE ...

  3. 窥探Vue.js 2.0 - Virtual DOM到底是个什么鬼?

    引言 你可能听说在Vue.js 2.0已经发布,并且在其中新添加如了一些新功能.其中一个功能就是"Virtual DOM". Virtual DOM是什么 在之前,React和Em ...

  4. jQuery学习之路(8)- 表单验证插件-Validation

    ▓▓▓▓▓▓ 大致介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 ...

  5. .Net 大型分布式基础服务架构横向演变概述

    一. 业务背景 构建具备高可用,高扩展性,高性能,能承载高并发,大流量的分布式电子商务平台,支持用户,订单,采购,物流,配送,财务等多个项目的协作,便于后续运营报表,分析,便于运维及监控. 二. 基础 ...

  6. Node.js npm 详解

    一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...

  7. C#开发微信门户及应用(39)--使用微信JSSDK实现签到的功能

    随着微信开逐步开放更多JSSDK的接口,我们可以利用自定义网页的方式来调用更多微信的接口,实现我们更加丰富的界面功能和效果,例如我们可以在页面中调用各种手机的硬件来获取信息,如摄像头拍照,GPS信息. ...

  8. 访问者模式(visitorpattern)

    /** * 访问者模式 * @author TMAC-J * 在客户端和元素之间添加一个访问者 * 当你需要添加一些和元素关系不大的需求时,可以直接放在访问者里面 * 或者是元素之间有一些公共的代码块 ...

  9. NYOJ 455

    1.应该交代清楚,参加宴会的人不知道一共有多少顶帽子.假如知道有n顶帽子的话,第一次开灯看见有n-1只,自然就知道自己是第n顶黑帽子,所以应该是这n个人在第一次关灯就打自己脸,不过这么一来就没意思了, ...

  10. 【Java并发编程实战】----- AQS(一):简介

    在前面博客中,LZ讲到了ReentrantLock.ReentrantReadWriteLock.Semaphore.CountDownLatch,他们都有各自获取锁的方法,同时相对于Java的内置锁 ...