python 拾贝
1.
内建的 type() 函数带三个参数时, 将作为强悍的动态类构造器. 如下:
type(name, bases, dict)
返回一个新的type对象. 基本上是 class 语句的动态形式. 参数:
例如,下面两条语句作用完全一样:
注意:该特性需要 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();
最新文章
- CSS3过渡、变形和动画
- HDU 4473 Exam 枚举
- iOS开发入门教程
- 10.31 afternoon
- ECSTORE 关于FILTER条件所代表的含义
- Swift--集合类型 数组 字典 集合
- oracle转换数字到格式化字符串
- 安装Oracle时可能碰到的常见问题-1
- Eclipse代码字体、颜色美化,更改字体大小、颜色
- 漫话JavaScript与异步·第二话——Promise:一诺千金
- 必应app测试
- 【一天一道LeetCode】#73. Set Matrix Zeroes
- 内置函数(sorted、map、enumerate、filter、reduce)
- DNS 负载均衡
- DOM知识点总结
- threejs- z-fighting 问题(模型的重叠部位便不停的闪烁起来。这便是Z-Fighting问题)
- linux下根目录扩容
- GIS-007-Terrain跨域访问
- winform messagebox 统一
- js 正则(自己一点点的笔记)