Python 函数

1. os.linesep是干什么的?
os.linesep字符串给出当前平台使用的行终止符。例如,Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。

2.解释器行为吧,被解释器引用计数了
int会 float不会
>>> a=5
>>> b=5
>>> a is b
True

>>> a=1.1
>>> b=1.1
>>> a is b
False

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

8.将一个二维列表平坦化
>>> list = [[1, 3, 5], [2, 4]]
>>>print [flatten for inner in list for flatten in inner]
[1, 3, 5, 2, 4]

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('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('&quot;', '\"').replace("&#39;", '\'')

18.文件以指定格式打开
fp = open(msgfile,encoding='gb18030')


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值