python 学习记录2

python中os模块
通过os模块调用系统命令,可以跨平台使用
1.os.getcwd()返回当前绝对路径,返回类型为str
import os
a=os.getcwd()
print a
2.os.mkdir()创建目录
os.remove(path)只能删除文件
os.rmdir(path)只能删除目录
3.os.path 主要针对路径的操作
os.path.abspath(path)返回绝对路径,主要有引号
pring os.path.abspath('.')
os.path.basename(path)返回文件名
os.path.dirname(path)返回文件路径,不包含文件名
os.path.exists(path)判断路径是否存在,存在返回true,不存在返回false


将网页的图片进行下载
#coding=GBK
import requests
import os

url='http://c.hiphotos.baidu.com/image/pic/item/09fa513d269759eeef490028befb43166d22df3c.jpg'

root="D://pics//"
path=root+url.split('/')[-1]  #以/分割字符串,保留最后一段

try:
    if not os.path.exists(root):
        os.mkdir(root)
    if not os.path.exists(path):
        r=requests.get(url)
        with open(path,'wb') as f: #wb,以二进制写模式打开  rb以二进制度模式打开
            f.write(r.content)    #下载
            f.close()
            print('success')
    else:
        print('already exist')
except:
    print('error')

小文件下载
import requests
url=' '
r=requests.get(url)
with open("1.jpg",'wb')as f:
    f.write(r.content)
大文件下载
import requests
url=' '
r=requests.get(url,stream=True)
with open("python.pdf",'wb')as pdf:
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:
            pdf.write(chunk)

 

将百度贴吧一页的图片下载到本地

#coding=GBK

import urllib
import urllib.request
from bs4 import BeautifulSoup

def get_content(url):
    html=urllib.request.urlopen(url)
    content=html.read().decode('utf-8')
    html.close()
    return content
    
def get_image(info):
    soup=BeautifulSoup(info,"html.parser")
    all_image=soup.find_all('img',class_="BDE_Image")
    x=1
    for image in all_image:
        print(all_image)
        urllib.request.urlretrieve(image['src'],"D:\\python_work\\桌面图片\\%s.jpg"%(x))
        x+=1
url="https://tieba.baidu.com/p/2218566379"
info=get_content(url)
print(info)
get_image(info)

1.format方法
template.format(p0,p1,....k0=v0)
tplt = "{0:^10}\t{1:{3}^10}\t{2:^10}"   # {1:{3}^10} 1表示位置,{3}表示用第3个参数来填充,^表示居中,10表示占10个位置
print(tplt.format("排名","学校名称","总分",'*'))

2.Beautiful Soup中的Tag标签
最基本的信息组织单元,分别用<>和</>标明开头和结尾
from bs4 import BeautifulSoup
soup=Beautiful(demo,"html.parser")
soup.title
结果:<title>This is a python demo page</title>
tag=soup.a
tag
结果: 
<a class="py1"  href="http://www.baidu.com" id="link1">Basic Python</a>

3.findall()用法
findall(pattern,string,flags=0)
1.regular_vl=re.findall(r"docs","https://docs.python.org/3/whatsnew/3.6.html")
ptint(regular_vl)
#['docs'] 查找全部r标识代表后面是正则的语句,是什么就查找什么,没有输出[]空列表
2.符号^表示匹配以https开头的的字符串返回,
3.用$符号表示以html结尾的字符串返回,判断是否字符串结束的字符串
4.# [...]匹配括号中的其中一个字符
regular_v4 = re.findall(r"[t,w]h","https://docs.python.org/3/whatsnew/3.6.html")
print (regular_v4)
# ['th', 'wh']
5.“d”是正则语法规则用来匹配0到9之间的数返回列表
regular_v5 = re.findall(r"\d","https://docs.python.org/3/whatsnew/3.6.html")
regular_v6 = re.findall(r"\d\d\d","https://docs.python.org/3/whatsnew/3.6.html/1234")
print (regular_v5)
# ['3', '3', '6']
print (regular_v6)
# ['123']
6.小d表示取数字0-9,大D表示不要数字,也就是出了数字以外的内容返回
regular_v7 = re.findall(r"\D","https://docs.python.org/3/whatsnew/3.6.html")
print (regular_v7)
# ['h', 't', 't', 'p', 's', ':', '/', '/', 'd', 'o', 'c', 's', '.', 'p', 'y', 't', 'h', 'o', 'n', '.', 'o', 'r', 'g', '/', '/', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '/', '.', '.', 'h', 't', 'm', 'l']

7.“w”在正则里面代表匹配从小写a到z,大写A到Z,数字0到9
regular_v8 = re.findall(r"\w","https://docs.python.org/3/whatsnew/3.6.html")
print (regular_v8)
#['h', 't', 't', 'p', 's', 'd', 'o', 'c', 's', 'p', 'y', 't', 'h', 'o', 'n', 'o', 'r', 'g', '3', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '3', '6', 'h', 't', 'm', 'l']
8.“W”在正则里面代表匹配除了字母与数字以外的特殊符号
regular_v9 = re.findall(r"\W","https://docs.python.org/3/whatsnew/3.6.html")
print (regular_v9)
# [':', '/', '/', '.', '.', '/', '/', '/', '.', '.']


贪婪匹配
*? 前一个字符0次或无限次扩展,最小匹配
+?前一个字符1次或无限次扩展,最小匹配
?? 前一个字符0次或1次扩展,最小匹配
{m,n}? 扩展前一个字符m至n次(含n),最小匹配

split()函数
str.split(str=" “,numstring.count(str))[n]
num表示分割次数,如果存在参数num,则仅分割成num+1个字符串,
[n]:表示选取第n个分片
https://s.taobao.com/search?q=%E4%B9%A6%E5%8C%85&imgfile=&ie=utf8
https://s.taobao.com/search?q=%E4%B9%A6%E5%8C%85&imgfile=&ie=utf8&bcoffset=3&ntoffset=3&p4ppushleft=1%2C48&s=44
https://s.taobao.com/search?q=%E4%B9%A6%E5%8C%85&imgfile=&ie=utf8&bcoffset=0&ntoffset=6&p4ppushleft=1%2C48&s=88

利用python解决的练习题:
1.利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456:

from functools import reduce
def str2float(s):
    def fn(x,y):
        return x*10+y 
    n=s.index('.')
    s1=list(map(fn,[x for x in s[:n]]))
    s2=list(map(fn,[x for x in s[n+1:]]))
    return reduce(fn,s1)+reduce(fn,s2)/10**len(s2)
print('\'123.4567\'=',str2float('123.4567'))


2.利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']:

def normalize(name):
    return name[0].upper()+name[1:].lower()
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)

3.
假设我们用一组tuple表示学生名字和成绩:

L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
请用sorted()对上述列表分别按名字排序:
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
def by_name(t):    //t代表的是元组
    return t[0]
L2=sorted(L,key=by_name)
print(L2)
再按成绩从高到低排序:
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
def by_score(t):
    return -t[1]
L2=sorted(L,key=by_score)
print(L2)
4.类的相关知识
class Student(object):
    def __init__(self, name, score):
        self.name = name
        self.score = score
注意到_init_ 方法的第一个参数永远是self,表示创建的实例本身,因此,在_init_方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身
1.要让内部属性不被外部访问,可以把属性的名称前加上两个下划线__,在Python中,实例的变量名如果以__开头,就变成了一个私有变量(private),只有内部可以访问,外部不能访问
2.需要注意的是,在Python中,变量名类似__xxx__的,也就是以双下划线开头,并且以双下划线结尾的,是特殊变量,特殊变量是可以直接访问的,不是private变量,所以,不能用__name__、__score__这样的变量名。
3.判断一个变量是否是某个类型可以用isinstance()判断
>>> isinstance(a, list)
True
>>> isinstance(b, Animal)
True
>>> isinstance(c, Dog)
True
4.我们来判断对象类型,使用type()函数
>>> type(123)
<class 'int'>
>>> type('str')
<class 'str'>
>>> type(None)
<type(None) 'NoneType'>
 总是优先使用isinstance()判断类型,可以将指定类型及其子类“一网打尽”

  

1.map()函数
map()传入的第一个参数是f,即函数对象本身
>>> def f(x):
...     return x * x
...
>>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> list(r)
[1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
['1', '2', '3', '4', '5', '6', '7', '8', '9']
map()作为高阶函数,事实上它把运算规则抽象了。
2.reduce()函数
调用reduce()函数,需要引入  from functools import reduce
把序列[1, 3, 5, 7, 9]变换成整数13579
from functools import reduce
def fn(x,y):
    return x*10+y
reduce(fn,[1,3,5,7,9])
输出13579
reduce把一个函数作用在一个序列[x1, x2, x3, ...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是:reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
3.filter()函数
filter()函数的作用是从一个序列中筛选出符合条件的元素,由于filter()使用了惰性计算,所以只有在取filter()结果的时候,才会真正筛选并每次返回下一个筛出的元素。
4.sorted()数
sorted()函数也是一个高阶函数,它还可以接收一个key函数来实现自定义的排序,例如按绝对值大小排序:
>>> sorted([36, 5, -12, 9, -21], key=abs)
[5, 9, -12, -21, 36]
>>> sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower)
['about', 'bob', 'Credit', 'Zoo']
要进行反向排序,不必改动key函数,可以传入第三个参数reverse=True:
>>> sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower, reverse=True)
['Zoo', 'Credit', 'bob', 'about']

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值