标签:name 批量下载 utf-8 sql end encoding 成功 https timestamp
输出PI
#输出PI from math import pi print(pi) 3.141592653589793
日期和时间操作
from datetime import datetime t = 0 print(datetime.fromtimestamp(t)) 1970-01-01 00:00:00
#当前时间 import time print(time.time()) 1490681347.365674
from datetime import datetime t = time.time() print(t) print(datetime.fromtimestamp(t)) 1490681423.5541148 2017-03-28 06:10:23.554114
输出json
#将一个list列表对象,进行了json格式的编码转换 import json l = [‘iplaypython‘,[1,2,3], {‘name‘:‘xiaoming‘}] encoded_json = json.dumps(l) #将列表,进行json格式化编码 print (repr(l)) print (encoded_json) decode_json = json.loads(encoded_json) print (type(decode_json)) print (type(encoded_json))
连接数据库
#连接数据库 import pymysql print(pymysql.VERSION) conn = pymysql.Connect(host=‘localhost‘,user=‘root‘,passwd=‘qin‘,db=‘person‘,port=3306,charset=‘utf8‘) cur = conn.cursor() cur.execute("select * from dream") print (cur.rowcount)
数据库查询结果,用json返回:
#数据库查询结果,用json返回 import json import pymysql conn = pymysql.Connect(host=‘localhost‘,user=‘root‘,passwd=‘python‘,db=‘zzdb‘,port=3306,charset=‘utf8‘) cur = conn.cursor() cur.execute("select * from users") print (‘共有‘,cur.rowcount,‘条数据‘) users=[] data = {} results = cur.fetchall() for r in results: print(r[0],end=‘ ‘) print(r[1],end=‘ ‘) print(r[2],end=‘ ‘) print("---") person = {} person[‘id‘] = r[0] person[‘name‘] = r[1] person[‘age‘] = r[2] users.append(person) cur.close() conn.close() data[‘code‘] = 0 data[‘msg‘] = ‘成功‘ data[‘users‘] = users jsonStr = json.dumps(data) print(jsonStr)
读文件、写文件
#读文件 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(‘\n‘+str(t2)) finally: if f: f.close()
图片操作
#把图片缩小到二分之一 from PIL import Image, ImageFilter im = Image.open(‘1.jpg‘) w, h = im.size print(‘原图片尺寸: %sx%s‘ % (w, h)) # 缩放到50%: im.thumbnail((w//2, h//2)) # 把缩放后的图像用jpeg格式保存: im.save(‘2.jpg‘, ‘jpeg‘) #要详细了解PIL的强大功能,请请参考Pillow官方文档: https://pillow.readthedocs.org/
操作EXCEL文件
#写EXCEL文件 # -*- coding: utf-8 -*- import xlwt book = xlwt.Workbook(encoding = "utf-8", style_compression = 0)#创建一个Wordbook对象,相当于创建了一个Excel文件 sheet = book.add_sheet("sheet1", cell_overwrite_ok = True)#创建一个sheet对象,一个sheet对象对应Excel文件中的一张表格 #向表sheet1中添加数据 #sheet.write(0, 0, "1行1列") #sheet.write(1, 0, "2行1列") #sheet.write(0, 1, "1行2列") #sheet.write(1, 1, "2行2列") for i in range(0,30): sheet.write(i,0,i) sheet.write(i,1,i*2) sheet.write(i,2,i*3) #最后,将以上操作保存到指定的Excel文件中 book.save("zz.xls")
#读EXCEL文件的内容 import xlrd xls_file = "zz.xls" book = xlrd.open_workbook(xls_file)#打开指定文件 sheet1 = book.sheet_by_index(0)# 通过sheet索引获得sheet对象 # 获得行数和列数 nrows = sheet1.nrows# 总行数 ncols = sheet1.ncols#总列数 # 遍历打印表中的内容 for i in range(nrows): for j in range(ncols): cell_value = sheet1.cell_value(i, j) print(cell_value, end = "\t") print("")
网易云音乐批量下载
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 + ‘ 下载完成‘)
..
..
标签:name 批量下载 utf-8 sql end encoding 成功 https timestamp
原文地址:http://www.cnblogs.com/qq21270/p/6655529.html