python学习笔记(六)

1) os模块。 os模块的path.split()方法可以将路径拆分成“路径部分”和“文件部分”,而path.splitext()方法可以将路径拆分成“非扩展名部分”和“扩展名部分”。path.abspath()方法可以将相对路径转换成绝对路径。因为在window下路径是用\分隔的,而在linux下,路径用/分隔,为了兼容不同系统,所以常常用os.path.join()方法来连接路径,os.path.join()会自动识别当前系统,选用合适的分隔符,从而做到跨系统兼容。
======================================
import os
a = "/etc/abc.txt"
os.path.split(a)           #=> ("/etc","abc.txt")
os.path.splitext(a)      #=> ("/etc/abc",".txt")
======================================

2) urllib模块。 和ruby一样,open只能读本地文件,如果要抓取网页,需要导入模块。
======================================
import urllib
str = urllib.urlopen("http://www.baidu.com").read()
print str
======================================

3) ConfigParser模块。 ConfigParser模块是用来处理配置文件的。将配置项专门放到一个配置文件里是个好习惯,用ConfigParser模块可以很方便地对配置文件进行修改。相应的,配置文件也要遵循一个标准格式。ConfigParser有read()方法,用于读取配置文件,sections()方法,用于获取所有小节,options(section)用于获取指定小节的所有配置项,get(section,option)获取指定配置项的值,set(section,option,value)用于设置指定配置项的值。
======================================
配置文件 abc :

[portal]
url = http://%(host)s:%(port)s/Portal
username = adang
host = localhost
password = 1234
port = 80

主文件:
from ConfigParser import ConfigParser
config = ConfigParser()
config.read("abc")
url = config.get("portal","url")
print url                    #=> http://localhost:80/Portal
======================================

4)time模块。 和时间相关的操作。

import time
time.time()           取得时间戳
time.ctime()         可读性稍好一些的字符串
time.localtime()    返回当前时间域的当前时间,可读性一般
time.localtime().tm_mday    返回月分
time.localtime().tm_wday    返回星期
time.localtime().tm_yday     返回年份

6) ElementTree模块。 处理XML的模块。

import xml.etree.ElementTree

7) random模块。 随机模块。

import random
random.randint(n,m)                         生成n~m之间的随机整数
random.randrange(n,m,l)                  生成n~m-1之间的随机数,数字之间的间隔为l
如random.randrange(20,201,2)        生成20~200之间的偶数
random.random()                              不能传参,生成0~1之间的浮点数
random.uniform(n,m)                         生成n~m之间的浮点数
random.shuffle(arr)                            给列表重新洗牌。对列表是破坏性的操作。
如arr = [1,2,3] ; random.shuffle(arr)

8) webbrowser模块。 此模块可以处理浏览器相关的事情。

import webbrowser
webbrowser.open('http://www.yahoo.com')        #=> 打开浏览器,跳到www.yahoo.com页面

9) shelve模块。 此模块有点像js控制cookie一样,它可以负责将数据持久化,不同的是,它是保存在一个文件而不是cookie中。此文件直接打开不具可读性,只能用模块自己来保存和查看。shelve自己处理了数据的序列化和反序列化,可以把它保存的数据理解为一个字典对象。

import shelve
s = shelve.open("mybase")
s["name"] = "adang"
s["sex"] = "male"
print s       {'name':'adang','sex':'male'}

10) re模块。 此模块可以用来处理简单的正则表达式,可用search和match方法于“检测”和“匹配”。

import re

m = re.match(r'foo','seafood')

if m is not None: print m.group()             #=>

print m                                                    #=> None

m = re.search(r'foo','seafood')

if m is not None: print m.group()             #=> 'foo'

m = re.search(r'\d','ab23c')                   

if m is not None: print m.group()             #=> '2'

转载于:https://www.cnblogs.com/cly84920/archive/2010/07/14/4426737.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值