1、AttributeError: 'module' object has no attribute 'text_format' 解决方案来自:http://blog.csdn.net/allyli0022/article/details/70888933
protobuf版本问题,应该是昨天安装keras时更新造成的,调回2.6成功
sudo pip install protobuf == 2.6.0
2、Python 'takes exactly 1 argument (2 given)' 解决方案来自:http://www.111cn.net/phper/python/125188.htm
原来在python中,在使用instance调用其class的方法的时候,是相当与在调用中加入本身作为第一个参数的。如下:
a.method(k)
就相当于:
a.method(a, k)
所以,在对函数进行定义的时候,需要在函数内部参数中第一个定义为self,如:
class Person():
def method(self, k):
...
如果想作为静态函数调用,可以在定义函数上方加上“@staticmethod”来进行标识。
3、给数字前面自动补0,参考:http://www.cnblogs.com/zhanglianbo/p/6202683.html
int型:('%06d' %k)
string型:k.zfill(6)
n = "123" s = n.zfill(5) assert s == '00123'n = 123 s = '%05d' % n assert s == '00123'
4、Python rindex()方法,返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常,你可以指定可选参数[beg:end]设置查找的区间。
5、python中添加中文注释需要在py文件开始部分加两行
#!/usr/bin/env python
# vim:set fileencoding=utf-8:
6、全局变量的定义
1)在文件开头定义变量并赋值,然后在函数中使用时使用global关键字声明一下
FP = 0
def vs():
global FP
FP = FP + 1
2) 定义一个全局变量的文件,然后使用对象调用
global.py
gl_a = 0
gl_b = 'hello'
test.py
import global as gl
print gl.gl_a
7、多行注释要注意缩进问题,否则会出现如下错误
IndentationError: unexpected indent
8、空格和tap不要混合使用!空格和tap不要混合使用!空格和tap不要混合使用!
9、排序http://www.jb51.net/article/57678.htm
10、pip安装不进入python环境(windows),直接在cmd下使用