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();

 

python 拾贝的更多相关文章

  1. python: 爬取[博海拾贝]图片脚本

    练手代码,聊作备忘: # encoding: utf-8 # from __future__ import unicode_literals import urllib import urllib2 ...

  2. [Python]新手写爬虫全过程(已完成)

    今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...

  3. [Python]新手写爬虫全过程(转)

    今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...

  4. Python+Flask+MysqL的web建设技术过程

    一.前言(个人学期总结) 个人总结一下这学期对于Python+Flask+MysqL的web建设技术过程的学习体会,Flask小辣椒框架相对于其他框架而言,更加稳定,不会有莫名其妙的错误,容错性强,运 ...

  5. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  6. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  7. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  8. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  9. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

随机推荐

  1. JMeter学习-030-JMeter性能测试常用之事务控制器实例

    通常进行性能测试时,我们一般仅考虑主要的数据返回,不考虑页面渲染所需要的数据(例如:css.js.图片等).但当我们需要衡量打开一个页面(页面渲染完成)的性能时,我们就需要考虑完成页面渲染所需要的图片 ...

  2. 初识The Battle of Polytopia

    1.首先了解了一下<文明5-美丽新文明>视频介绍网址:http://list.youku.com/albumlist/show?id=19481409&ascending=1&am ...

  3. 一次性搞明白 service和factory区别

    原文链接 http://blog.thoughtram.io/angular/2015/07/07/service-vs-factory-once-and-for-all.html 等下,已经有一篇文 ...

  4. .NET 串口通信中断接收,包含0X1A(作为EOF)

    .NET串口通信中将`0X1A`当做EOF处理,.NET接收到EOF会触发一次接收中断,此时事件形参`SerialDataReceivedEventArgs`值为枚举 `Eof`,其他为`Chars` ...

  5. 使用T4模板生成代码的学习

    之前做项目使用的都是Db First,直接在项目中添加Entity Framework,使用T4模板(T4模板引擎之基础入门)生成DAL BLL层等(T4模板是一个同事给的,也没有仔细研究,代码如下: ...

  6. 使用命令行 Subversion 访问项目源文件(SVN)

    from:http://www.open.collab.net/scdocs/ddUsingSVN_command-line.html.zh-cn 命令行 Subversion 入门 如果您参与的项目 ...

  7. Struts2批量验证(POC)

    only poc , 再据结果利用EXP进一步测试: 支持 -u 单个url; -f 文本批量URL导入 url列表格式是https://www.baidu.com #! /usr/bin/env p ...

  8. 论文阅读(Weilin Huang——【TIP2016】Text-Attentional Convolutional Neural Network for Scene Text Detection)

    Weilin Huang--[TIP2015]Text-Attentional Convolutional Neural Network for Scene Text Detection) 目录 作者 ...

  9. JAVA反射参数传递

    引用:http://fish2700.blog.163.com/blog/static/130713192009103035723281/ 使用Method反射调用函数时,我们通常会遇到以下几种情况: ...

  10. scale配合过渡的时候bug

    使用scale的时候注意两点 1:scale(1)的时候尽量图片的 width==naturalWidth bug表现为过渡生效时候图片变模糊 2:scale在过渡前和过渡后的计算后的width和he ...