diff --git a/README.md b/README.md index a4e5249..0e1529b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,89 @@ # Python Study -> 关于Python工作中的一些总结性技术 +> 控制台打印乱码: **` print '你好,世界!'.decode('utf-8') `** + +```python +url = 'http://{0}:{1}/{2}'.format('0.0.0.0', 2375, 'xxx') +url = 'http://{ip}:{port}/{uri}'.format(ip='0.0.0.0', port=2375, uri='xxx') +url = 'http://%s:%d/%s' % ('0.0.0.0', 2375, 'xxx') +``` + +## Windows Python 依赖库[ **PythonLibs**](http://www.lfd.uci.edu/~gohlke/pythonlibs/) +* 1.找到对应的 `whl` 包下载 +* 2.直接`pip install *.whl` 或者修改`.whl`文件为`.zip`文件,解压缩文件的`Python文件夹`复制到--`python`安装目录下的`Lib`--目录下 + +## [Python 中文翻译文档集合](http://python.usyiyi.cn/) +## [Python 官方文档](https://docs.python.org/2.7/) +## [Top Python APIs](https://www.programcreek.com/python/index/module/list) + +## Python2.7环境变量 +> 假如`sys.path`不对,则使用Python终端 ` sys.path = [...] `重新设置即可. +> 默认环境配置如下: + +```shell +root@node-40:~# python +Python 2.7.6 (default, Jun 22 2015, 17:58:13) +[GCC 4.8.2] on linux2 +Type "help", "copyright", "credits" or "license" for more information. +>>> import sys +>>> sys.path +['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7'] +>>> +``` +```shell +# /etc/profile + +export PYTHONPATH=/usr/lib/python2.7:/usr/lib/python2.7/plat-x86_64-linux-gnu:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PILcompat:/usr/lib/python2.7/dist-packages/gtk-2.0:/usr/lib/pymodules/python2.7 +export PATH=$PATH:$PYTHONPATH +``` + +## Windows环境`Python2.7`与`Python3.x` 共同使用 + +* Python2.7 : `$ py -2` +* Python3.x : `$ py -3` +* Python2.7 pip : `$ py -2 -m pip xxx` +* Python3.x pip : `$ pip3 xxx` + +## pycharm +> settings + +* enable Code compatibility inspection: `settings` --> `code compatibility inspection` + +## Python内置工具 + +* 下载服务器: + * Python2.x + * `$ python -m SimpleHttpServer` 默认端口8000 + * `$ py -2 -m SimpleHTTPServer` 默认端口8000 + * `$ py -2 -m SimpleHTTPServer 9090` 指定端口9090 + * 使用代码: + ```python + import SimpleHTTPServer + + SimpleHTTPServer.test() + ``` + * Python3.x + * `$ python -m http.server` + * `$ py -3 -m http.server` + +* Json格式化:`$ curl http://localhost:8080/get | python -m json.tool` + +* 执行Python代码:`$ python -c "print 'hello world!'"` + +* 解压zip包: + * 创建zip包:`$ python -m zipfile -c tom.zip tom.txt` + * 解压zip包:`$ python -m zipfile -e tom.zip .` + * 查看zip包:`$ python -m zipfile -l tom.zip` + + +* 文件处理: + ```python + import shutil + + shutil.copy('C:\Users\Administrator\Desktop\ctools2.rar','q.rar') + ``` + + +## 关于Python工作中的一些总结性技术 * [爬虫](https://github.com/tomoncle/PythonStudy/tree/master/crawlers/) * [RPC](https://github.com/tomoncle/PythonStudy/tree/master/rpc/) @@ -14,5 +98,4 @@ * [Python 多线程/多进程](https://github.com/tomoncle/PythonStudy//tree/masterstandard_library/threads/) * [Python 内置模块](https://github.com/tomoncle/PythonStudy/tree/master/standard_library/) * [Python 使用技巧](https://github.com/tomoncle/PythonStudy/tree/master/skills) -* .... diff --git a/skills/README.md b/skills/README.md index 92f3cdb..c363f98 100644 --- a/skills/README.md +++ b/skills/README.md @@ -1 +1,66 @@ # python 中一些常用的技巧 + +##### 转义 +* Python中`"%"`的转义是`"%%"` + +##### float类型保留小数: +```python +b = float('%0.6f'%0.12345678) +print b #0.123457 +``` + +##### url检测 +```python +url = 'https://www.baidu.com/' + +print url.rstrip('/') +print url.rstrip('/')+'/home' +``` + +##### url 转换函数 +```python +import urlparse +urlparse.urljoin('http://www.aa.com:90/aa.html', '/abc.html') +# http://www.aa.com:90/abc.html + + +from __future__ import print_function + +import urlparse + +u = urlparse.urlparse('029_t002661t9gt.321002.2.ts?index=29&start=310000&end=320400') +query_params = dict([s.split('=') for s in u.query.split('&')]) +print('query_params : {}'.format(query_params)) +# query_params : {'index': '29', 'end': '320400', 'start': '310000'} +``` + +##### 去空格 +```python +s = ' 1 2 3 4 5 6 ' + +print '|%s|' % s.lstrip(' ') # 去除左边空格 |1 2 3 4 5 6 | +print '|%s|' % s.rstrip(' ') # 去除右边空格 | 1 2 3 4 5 6| +print '|%s|' % s.strip(' ') # 去除两边空格 |1 2 3 4 5 6| +print '|%s|' % s.replace(' ', '') # 去除所有空格 |123456| +``` + +##### 显示有限的接口到外部 +``` +当发布python第三方package时, 并不希望代码中所有的函数或者class可以被外部import, +在__init__.py中添加__all__属性, +该list中填写可以import的类或者函数名, 可以起到限制的import的作用, 防止外部import其他函数或者类 +``` + +```python +#!/usr/bin/env python +# -*- coding: utf-8 -*- +from base import utils + +__all__ = ['utils'] +``` + +--- +## Python博客 +* 关于`raw_input()` 和 `input()` :http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html + +