101:Python基础 第一章: 绪论2-参考

常用函数介绍:

help(“sys”)

函数的hlep文档

range(end)

range  (start, end)

range  (start, end, step)

range(10) 表示0…9

dir()

当前命名空间的所有标识符

len(“hi”)

序列的长度

“str” * intValue

“a” * 3  得到“aaa”

raise excName, excInfo

抛异常

issubclass(class, fatherClass)

是否子类关系

isinstance(object, class)

是否为类的实例对象

__getattr__

 

__getattribute__

访问一个属性名,先在__dict__属性找,找不到再到这个方法里找.

每次访问属性都会执行它。重写要调用基类的方法,用__dict__回无穷递归(用__dict__属性也会调此方法)。

注释:当obj.Attr来访问,先执行__getattribute__方法,或再执行__getattr__

ord(“z”)

返回字符的unicode代码

string的center(空格数),rjust(空格数),ljust(空格数)

字符串的格式化:居中,左,右对齐

string的strip (),lstrip (),rstrip ()

字符串的去掉空格和换行符

import os

os.environ.keys():

os.getcwd()

os.chdir(‘c:\\a\\b’)

得到OS操作系统的信息

得到当前目录

 

import csv

csv.reader(u)

读取CSV文件的内容,或者处理字符串

eval(str)

可以把格式”str”变成str。

type (obj)

判断属于类

 

常用常量介绍:

__name__

模块名,比如 "__main__"

__bases__

显示类的直接父类及其内存等信息

__dict__

返回以字典,值为命名空间所有标识符

__slots__

类中列出所有允许包含的属性。否则会抛异常。会给未赋值的赋值为None。

 

 

 

示范代码:

def gcd( x, y ):

   while y:

      z = x

      x = y          

      y = z % y

 

   return x

求最大公约数

import urllib

import re

 

def gethtmlSource():

    tempFile = urllib.urlopen( "http://image.baidu.com/" )

    strPageSource =  tempFile.read()

    return strPageSource

def getimage(htmlSource):

    pattern = re.compile(r'"src": "(http.*?jpg)","url"')

    imglist = re.findall(pattern, htmlSource)

    iimge = 0

    for oneulr in imglist:

        oneulr = re.sub(r"\\", "", oneulr)

        iimge += 1

        urllib.urlretrieve(oneulr, "C:/\ image/\%s.jpg" % iimge)

      

getimage (gethtmlSource())

一个小爬虫的小程序。

http://image.baidu.comjpg的图像下载到本地Cimage文件夹下面。

from tkMessageBox import showwarning

warn = lambda excel: showwarning(excel, 'Exit?')

warn(‘excel’)

弹出对话框

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值