您的位置:首页 > 编程语言 > Python开发

python学习整理

2015-06-02 10:44 459 查看
不要通过sys.argv[0]获得当前路径,那是不正确的。sys.argv[0]
是当前执行的Python脚本的文件名,不一定是当前的路径。"assert"
will be ignored in optimization (i.e. python -O). Please raise an Exception instead.if
you’re just wrapping or filling one or two text strings, the convenience functions should be good enough; otherwise, you should use an instance of
TextWrapper
for
efficiency#
-*- coding: utf-8 -*- //设置编辑器,默认保存为 utf-8 格式。允许python脚本中出现中文字符类名单词首字母大写,不使用下划线连接单词,也不加入
C、T 等前缀
http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=12014716&id=4326013
json的标准格式:要求必须
只能使用双引号作为键 或者 值的边界符号,不能使用单引号,而且“键”必须使用边界符(双引号)"{0:<9}".format(id) 格式化输出id的长度不超过9
print list(set(b).difference(set(a))) # b中有而a中没有的
print
list(set(a).union(set(b))) #获取两个list 的并集ret
= list(set(a) ^ set(b)) #获取两个list 的差集
f = e[:] //list e值深度拷贝到list f
ret
= []for i in a:if i not in b:ret.append(i)Template
>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
在python中 None,
False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False

dict.copy()
深度拷贝
help(dict)
两个辞典不同的关键字相加>>>
a = {"1": "a"}>>> b = {"2": "b", "3": "c"}>>> c = a.copy()>>> c.update(b)
dict.get(key, default=None) //The method get() returns a
value for the given key. If key is not available then returns default value None.
diff = set(dictb.keys()) - set(dicta.keys()) //两个辞典的差集
把字典的key转换成列表,使用dict.keys()就可以了
__call__可以让对象直接像函数一样使用,类似于重载圆括号
回调函数:直接把函数名作为参数传给调用函数
pass
//空语句
import
subprocess
subprocess.Popen(["mount", "nfs:/point", mount_point]) 挂载NFS挂载点
subprocess.Popen(["umount",
mount_point])mkdir
//only creates a single sub-directory,
makedirs
// creates
all the intermediate directories if they don't exist (just like
mkdir
-p
in bash)
print
self.__doc__ //打印class 下面的描述信息
如果对应参数没有出现,则是默认store_true/store_false的相反值,如果对应的参数出现,就是存储在里面的值。所以一般情况下action用store_true就对了,参数不存在就返回false,参数存在就返回true。设置
add_option 方法中的 metavar 参数,有助于提醒用户,该命令行参数所期待的参数,如 metavar=“mode”:

print
"Let's talk about %s." % my_name //print 打印参数值

str_split=str.split('.',1)
//按str字符.分割1次
"".join(s.split()) #去掉s字符串中的空格

b,c
= a.split("-", 1) //b,c分别为分隔前后的值

sys.path.insert(0,'/usr/src') //把搜索路径放到第一位sys.path.append('/usr/src/CamProject') //把搜索路径放到最后一位如果我们需要导入的模块是放在文件夹里面,那么,文件夹里面必须要添加__init__.py文件,以使得python能够找到模块的位置。__init__.py可以为空,只要它存在,就表明此目录应被作为一个package处理。argparse参数意义:type(指定存储类型)和dest(指定存储变量)
base64 – Encode binary data into ASCII characters
>>> import commands>>> commands.getstatusoutput('ls
/bin/ls')(0, '/bin/ls')
//获取命令执行返回值
os.path.isfile(path) //This
function returns true if the given path is an existing, regular file.
以单下划线开头(_foo)的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用"from
xxx import *"而导入;以双下划线开头的(__foo)代表类的私有成员;以双下划线开头和结尾的(__foo__)代表python里特殊方法专用的标识,如__init__()代表类的构造函数。
python
-mrequests.certs
os.rename("/tmp/1",
"/tmp/2")当你导入一个模块,Python解析器对模块位置的搜索顺序是:当前目录如果不在当前目录,Python 则搜索在 shell 变量 PYTHONPATH 下的每个目录。如果都找不到,Python会察看默认路径。UNIX下,默认路径一般为/usr/local/lib/python/。模块搜索路径存储在system模块的sys.path变量中。变量里包含当前目录,PYTHONPATH和由安装过程决定的默认目录。using airspeed to generate txtloader = airspeed.CachingFileLoader("clients/gentxt")install_template = loader.load_template("install.template")install_template_txt = install_template.merge(self.install_template_dict, loader=loader)sudo pip install requests --upgrade #升级requests版本pip freeze #查看当前安装包版本wget https://pypi.python.org/packages/source/p/pykerberos/pykerberos-1.1.7-1.tar.gz
tar zxf pykerberos-1.1.7-1.tar.gzcd pykerberos-1.1.7# edit setup.py like vi setup.pypython setup.py buildpython setup.py installTo record list of installed files, you can use:
python setup.py install --record files.txt
Once you want to uninstall you can use xargs to do the removal:
cat files.txt | xargs rm -rf

python -V or python --version//查看python版本install pycharm
sudo add-apt-repository ppa:mystic-mirage/pycharm

sudo apt-get update
sudo apt-get install pycharm-community>>> import sys>>> print sys.pathsession = requests.Session()session.post('http://xlzd.me/login',
data={'user':'xlzd','pass':'mypassword'})#
登录成功则可以发布文章了
session.put('http://xlzd.me/new',data={'title':'title
of article', 'data':'content'})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: