• 学习笔记:python3,代码。小例子习作


    http://www.cnblogs.com/qq21270/p/7634025.html  学习笔记:python3,一些基本语句(一些基础语法的代码,被挪到这里了)

    日期和时间操作

    http://blog.csdn.net/ibiao/article/details/78067497  time与datetime

    http://www.cnblogs.com/shhnwangjian/p/6117602.html  python 时间类型和相互转换

    复制代码
    # -*- coding: utf-8 -*-
    # coding=utf-8
    import time
    from datetime import datetime   #这个要from引用,否则 datetime.fromtimestamp 会报错
    dt = time.time()        # 获取当前的时间戳         1507384502.2770934
    t0 = time.gmtime()       # 将时间戳转换成结构化时间元组(UTC时区)伦敦时区    time.struct_time(tm_year=2017, tm_mon=10, tm_mday=7, tm_hour=14, tm_min=0, tm_sec=39, tm_wday=5, tm_yday=280, tm_isdst=0)
    t1 = time.localtime()    # 将时间戳转换成结构化时间元组(UTC+8时区)北京时区      time.struct_time(tm_year=2017, tm_mon=10, tm_mday=7, tm_hour=22, tm_min=0, tm_sec=39, tm_wday=5, tm_yday=280, tm_isdst=0)
    print(dt)
    print(t0)
    print(t1)
    print(datetime.fromtimestamp(dt))           #2017-10-07 22:00:39.674642
    print(time.strftime('%Y-%m-%d %H:%M:%S'))   #2017-10-07 22:00:39
    print(time.gmtime(1498488176.3209014))  #time.struct_time(tm_year=2017, tm_mon=6, tm_mday=26, tm_hour=14, tm_min=42, tm_sec=56, tm_wday=0, tm_yday=177, tm_isdst=0)
    复制代码
    def __str2time(s):      #一个字符串转换为datetime的时间函数
        t = time.strptime(s,'%Y-%m-%d %H:%M:%S')
        return datetime(*t[:6])
    
    s = __str2time("2017-9-29 22:10:15")
    print(s)           #2017-09-29 22:10:15 <class 'datetime.datetime'>
    复制代码
    # 计算起止时间
    import time, random
    start = time.time()
    time.sleep(random.random())
    end = time.time()
    print('runs %0.5f seconds.' %(end - start))
    print('runs %0.2f seconds.' %(end - start)) #取小数点后2位
    print('runs %0.1f seconds.' %(end - start))
    复制代码
    t1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  #日期格式化
    print(t1)

    读文件、写文件

    复制代码
    #读文件
    f = open("c:\1.txt","r")
    lines = f.readlines()
    for line in lines:
       print( line)
    
    #写文件
    f = open("c:\1.txt","r+")
    f.write("123")#写入字符串
    复制代码
    复制代码
    #每运行一次,追加一下更改时间
    import time
    from datetime import datetime
    try:
        f = open('1.txt', 'r+')
        sss = f.read()
        print(sss)
        t2 = datetime.fromtimestamp(float(time.time()))
        f.write('
    '+str(t2))
    finally:
        if f:
            f.close()
    复制代码

    下载大文件  2017-10-7      补充阅读:http://blog.topspeedsnail.com/archives/8462  多线程下载

    复制代码
    import requests
    import urllib
    #openurl = "http://www.xxx.com/zz.rar"     #普通下载
    #saveurl = "d:/99999.rar"
    #urllib.request.urlretrieve(openurl, saveurl)
    
    openurl = "http://xxx/xxx.tar.gz"
    saveurl = "d:/999.tar.gz"
    r = requests.get(openurl, stream=True)    #流式下载
    f = open(saveurl, "wb")
    for chunk in r.iter_content(chunk_size=512):
        if chunk:
            f.write(chunk)
    复制代码

    网易云音乐批量下载

    https://www.zhihu.com/question/20799742

    一个例子。建立文件夹后,python下载音乐文件。  2017-3-23 

    复制代码
    # -*- coding: utf-8 -*-
    import requests
    import urllib
    
    # 榜单歌曲批量下载
    #http://music.163.com/discover/toplist?id=3779629    id来自于 http://music.163.com/ 的“云音乐新歌榜”
    r = requests.get('http://music.163.com/api/playlist/detail?id=3779629')
    arr = r.json()['result']['tracks']    # 共有100首歌
    
    for i in range(10,20):    # 输入要下载音乐的数量,1到100。
        name = str(i+1) + ' ' + arr[i]['name'] + '.mp3'
        link = arr[i]['mp3Url']
        urllib.request.urlretrieve(link, '网易云音乐\' + name)    # 提前要创建文件夹
        print(name + ' 下载完成')
    复制代码

     http://moonlib.com/606.html  网易云音乐常用API  2018-3-31

    使用urllib,发送请求

    这里有点小问题还没解决,拿到的网页,解码有问题:    print(resp.read().decode("utf-8"))

    复制代码
    from urllib import request
    from urllib import parse
    url111="http://www.baidu.com"
    req = request.Request(url111)
    
    #冒充浏览器生成请求头
    req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3026.3 Safari/537.36")
    #使用 urlencode 生成POST数据
    postData = parse.urlencode([
        #("rsv_spt","1"),
        ("wd","444")
    ])
    #resp = request.urlopen(url111)
    #resp = request.urlopen(req)
    resp = request.urlopen(req,data=postData.encode("utf-8"))
    print(resp.status)
    print(resp.read())
    复制代码

    几个一行代码  https://www.zhihu.com/question/37046157

    # 一行筛质数
    n = 100
    a = filter(lambda x: all(map(lambda p: x % p != 0, range(2, x))), range(2, n))
    for i in a:
        print(i)
    # 碾平list
    a = [1, 2, [3, 4], [[5, 6], [7, 8]]]
    flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]
    print(flatten(a))
    # 求偶数的平方
    a = [x * x for x in range(20) if x % 2 == 0]
    print(a)

    ..

  • 相关阅读:
    Ubuntu下录音机程序的使用
    Bash中的数学计算
    Bash中的数学扩展
    Bash的命令替换
    top的用法
    VirtualBox的快照功能
    格式化输出和printf命令
    read命令读取用户输入
    Bash的作业控制
    Codeforces Round #455 (Div. 2)
  • 原文地址:https://www.cnblogs.com/valorchang/p/11282467.html
Copyright © 2020-2023  润新知