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

不错的有关Python 函数笔记(转)

2013-11-27 22:32 531 查看
原文地址:http://blog.csdn.net/oyzl68/article/details/8020982
1. os.linesep是干什么的?
os.linesep字符串给出当前平台使用的行终止符。例如,Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。

3.使用随机数
>>> import random
>>> print random.uniform(1,10)
3.65089367909

4.有的IDE可以打断点,PYDEV就可以,利用pdb可以单步调试

5.print smtplib.encode_base64('yi')

6.scapy ——网络包构建分析框架,可编程的wireshark,有兴趣的google “Silver Needle in the Skype”

7.python的安装
# rm -f /usr/bin/python
# ln -s ~/Desktop/python /usr/bin/python

9.查询某个导入类的方法
dir(email.generator)
help(email.generator.Generator)

10.打印当前时间
print (datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))

11.python3.0 中去掉了raw_input 函数,改用input。
12.python3.0 已经不支持了
apply(self.func,self.args)
改为
self.func(*self.args)

13.py3的encode与decode
>>> print ('中国'.encode('utf-8'))
b'\xe4\xb8\xad\xe5\x9b\xbd'
>>> print (b'\xe4\xb8\xad\xe5\x9b\xbd'.decode('utf-8'))
中国
>>>print (b'\u8bf7\u8f93\u5165\u95ee\u9898\u7b54\u6848'.decode('unicode-escape'))
请输入问题答案

subject='\u8bf7\u8f93\u5165\u95ee\u9898\u7b54\u6848'
subject = bytes(subject,'gb2312').decode('unicode-escape')
>>>print subject

解决中文通用办法
froms=froms.encode('raw_unicode_escape', 'ignore').decode('GB18030', 'ignore')

14.urlencode
>>> import urllib
>>> urllib.unquote("myemail%40gmail.com") #py2.7
>>>urllib.parse.unquote("myemail%40gmail.com") #py3.0

15.把一个py文件编译为pyc文件了。
import py_compile
py_compile.compile(r'H:\game\test.py') #windows环境下
py_compile.compile('wbm_sina.py') # Linux环境

16.时间格式化
>>> date='Wed, 11 Apr 2012 09:37:05 +0800'
>>> dd=datetime.datetime.strptime(date,'%a, %d %b %Y %H:%M:%S %z')
>>> dd.strftime('%Y-%m-%d %H:%M:%S')

17.html解码
#html和url解码
def HTMLDecode(html):
return html.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '\"').replace("'", '\'')

18.文件以指定格式打开
fp = open(msgfile,encoding='gb18030')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: