Python 语法

1、python split()函数使用拆分字符串 将字符串转化为列表

>>> u = "www.doiido.com.cn"

#使用默认分隔符
>>> print u.split()
['www.doiido.com.cn']

#以"."为分隔符
>>> print u.split('.')
['www', 'doiido', 'com', 'cn']

2、使用Python解析JSON数据的基本方法
从网页上下载json文件html,提出里面的title。

import json
jsonStr = json.loads(html)
title=jsonStr['title']

3、Python判断文件和文件夹是否存在的方法

>>> import os
>>> os.path.exists('d:/assist')
True
>>> os.path.exists('d:/assist/getTeacherList.py')
True
>>> os.path.isfile('d:/assist')
False
>>> os.path.isfile('d:/assist/getTeacherList.py')
True
>>> os.makedirs('d:/assist/set')
>>> os.path.exists('d:/assist/set')
True

4、Python教程:[61]创建文件夹/目录
判断’../src/temp/’文件夹是否存在,如果没有则建立相应文件夹

import os
if os.path.exists('../src/temp/')==False:
    os.mkdir('../src/temp/')

如果是多级目录,用os.makedirs()函数
5、Python爬虫关于urlretrieve()函数的使用笔记

import urllib
def cbk(a, b, c): 
    '''回调函数
    @a: 已经下载的数据块
    @b: 数据块的大小
    @c: 远程文件的大小
    ''' 
    per = 100.0 * a * b / c 
    if per > 100: 
        per = 100 
    print '%.2f%%' % per
url = 'http://www.google.com'
local = 'd://google.html'
urllib.urlretrieve(url, local, cbk)

url:远程文件地址
local:本地保存路径
cbk:会掉函数,显示下载进度

6、 python 3.3 try..catch 小例

s=input("input your age:")  
if s=="":  
    raise Exception("input must not be empty.")  

try:  
    i=int(s)  
except Exception as err:  
    print(err)  
finally:  
    print("Goodbye!")  

7、 Python中函数参数(默认、列表、可变长度、字典类型)
8、Python yield 使用浅析:由浅入深,讲的比较好

另一个 yield 的例子来源于文件读取。如果直接对文件对象调用 read() 方法,会导致不可预测的内存占用。好的方法是利用固定长度的缓冲区来不断读取文件内容。通过 yield,我们不再需要编写读文件的迭代类,就可以轻松实现文件读取:
清单 9. 另一个 yield 的例子

 def read_file(fpath): 
    BLOCK_SIZE = 1024 
    with open(fpath, 'rb') as f: 
        while True: 
            block = f.read(BLOCK_SIZE) 
            if block: 
                yield block 
            else: 
                return

9、Python 中的进程、线程、协程、同步、异步、回调

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值