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

来源:互联网 发布:淘宝儿童玩具电子琴 编辑:程序博客网 时间:2024/05/28 15:49
原文地址: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.650893679094.有的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/python9.查询某个导入类的方法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.015.把一个py文件编译为pyc文件了。import py_compilepy_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')