1.

内建的 type() 函数带三个参数时, 将作为强悍的动态类构造器. 如下:
 
type(name, bases, dict)

返回一个新的type对象. 基本上是 class 语句的动态形式. 参数:

name  , 字符串, 制定要构造类的名字, 赋给新对象的 __name__ 属性;

bases,一个tuple,指定新类型的所有基类,赋给新对象的__bases__ 属性;

dict, 字典类型,作为新类的名字空间,赋给新对象的__dict__ 属性.

例如,下面两条语句作用完全一样:

  1. >>> class X(object):
  2. ... a = 1
  3. ...
  4. >>> X = type('X', (object,), dict(a=1))

注意:该特性需要 Python >= 2.2.

 
type(object)

带一个参数返回 object 的类型. 返回一个 type 对象. 建议用内建的 isinstance() 函数来确定对象类型.

2.   内建函数 int, 可以用来将字符串通过指定进制数对应的十进制数

  例如  :   int('11', 8)  => 9       int('11', 4) => 5,   int('A', 16) => 10

class int(object)

|  int(x=0) -> int or long

|  int(x, base=10) -> int or long

|

|  Convert a number or string to an integer, or return 0 if no arguments

|  are given.  If x is floating point, the conversion truncates towards zero.

|  If x is outside the integer range, the function returns a long instead.

|

|  If x is not a number or if base is given, then x must be a string or

|  Unicode object representing an integer literal in the given base.  The

|  literal can be preceded by '+' or '-' and be surrounded by whitespace.

|  The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to

|  interpret the base from the string as an integer literal.

|  >>> int('0b100', base=0)

|  4

3.   字符  和    ASCII码 或  Unicode码  的相互转换

  ord('a')    =>    97              chr(97)   => 'a'      unichr(97)   => u'a'

  ord(u'郭')     =>  37101     unichr(37101)  =>  u'\u90ed'    (注: print u'\u90ed'    =>  郭     int('90ed',  16)   =>   37101

4.

>>> import ast

>>> ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}")

{'muffin': 'lolz', 'foo': 'kitty'}

>>> for i,j in enumerate(range(0,10)):

...  print i,j

...

0 0

1 1

2 2

3 3

4 4

5 5

6 6

7 7

8 8

9 9

5.

Use of a single underscore in IDLE.

An underscore represents the result of a previous expression

>>> 1 + 2

3

>>> _ + 1

4

6.

Circular lists :)

>>> def make_circular_list(a_list):

...   while True:

...     for an_item in a_list:

...       yield an_item

...

>>> a_list = [1,2,3]

>>> a_circular_list =  make_circular_list(a_list)

>>> a_circular_list.next()

1

>>> a_circular_list.next()

2

>>> a_circular_list.next()

3

>>> a_circular_list.next()

1

7.

Finding the most frequent element in a list, x:

max(set(x), key=x.count)

8.

编码错误解决方案:

(1).

import sys

reload(sys)

sys.setdefaultencoding('utf-8')

ErrorMsg = ErrorMsg.decode('utf-8')

return HttpResponse(ErrorMsg)

(2).

看起来,你这里的问题,其实是:

把文件内容,写入到文件中时,出错了。

而出错的原因其实是,python系统,在使用默认的编码类型,此处的ascii,去将对应的内容,写入到文件中。

但是由于其中一些内容,ascii编码不支持,所以报错。

所以,更好的办法是,在输出的时候,对文件制定特定的UTF-8编码即可。

而无需改动默认编码。

具体做法是:

不使用open打开文件,而使用codecs:

fp = codecs.open(‘output.txt’, ‘a+’, ‘utf-8′);;

fp.write(row[1]);

fp.close();

 

最新文章

  1. CSS3过渡、变形和动画
  2. HDU 4473 Exam 枚举
  3. iOS开发入门教程
  4. 10.31 afternoon
  5. ECSTORE 关于FILTER条件所代表的含义
  6. Swift--集合类型 数组 字典 集合
  7. oracle转换数字到格式化字符串
  8. 安装Oracle时可能碰到的常见问题-1
  9. Eclipse代码字体、颜色美化,更改字体大小、颜色
  10. 漫话JavaScript与异步·第二话——Promise:一诺千金
  11. 必应app测试
  12. 【一天一道LeetCode】#73. Set Matrix Zeroes
  13. 内置函数(sorted、map、enumerate、filter、reduce)
  14. DNS 负载均衡
  15. DOM知识点总结
  16. threejs- z-fighting 问题(模型的重叠部位便不停的闪烁起来。这便是Z-Fighting问题)
  17. linux下根目录扩容
  18. GIS-007-Terrain跨域访问
  19. winform messagebox 统一
  20. js 正则(自己一点点的笔记)

热门文章

  1. 手势响应 ,避免点击多个cell同时响应同一手势多次,只响应第一个cell
  2. 去掉tableview顶部留白
  3. C# 与数据库中字段类型 Int16(short), Int32(int), Int64(long)的取值范围、区别 。string长度
  4. python中常用的模块的总结
  5. 移动Web开发调研
  6. Web 服务器 low bandth DOS attack
  7. cs
  8. jquery+ajax实现分页
  9. springmvc中的controller是单例的
  10. android 案例二 登录界面