python_常用知识总结

目录

1.操作EXCEL

2.操作TXT

3.爬取网页

4.BeautifulSoup

5.随机数

6.时间类

7.移除列表元素

8.列表排序

9.append&extend

 10.优雅访问列表下标索引

11.对字典按照value进行排序 

12 自动备份mysql数据库脚本

13 字目和数字转换

14 dict初始化的方法

15 tuple

16 与输入顺序保持一致的字典

17 堆 


 


1.操作EXCEL

import xlrd

# 获取一个Book对象
workbook = xlrd.open_workbook("C:/bz/xa2018-05-01_14_55_15.xlsx")

# 获取一个sheet对象的列表
sheets = workbook.sheets()

# 遍历每一个sheet,输出这个sheet的名字(如果是新建的一个xls表,可能是sheet1、sheet2、sheet3)
for sheet in sheets:
    sheet_data = workbook.sheet_by_name(sheet.name)
    rows = sheet_data.row_values(0)
    cols=sheet_data.col_values(0)
    print(rows) #第一行
    print(cols) #第一列

 

import xlwt
import time

####写入excel
app = xlwt.Workbook() #创建工作簿
sheet1 = app.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheetapp

test=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]

for row in range(len(test)):
    for col in range(len(test[row])):
        sheet1.write(row, col, test[row][col])

t=time.strftime('%Y-%m-%d_%H_%M_%S',time.localtime(time.time()))
app.save("C:/bz/"+"test_"+t+".xlsx") #保存文件


2.操作TXT

import time
###写入数据
t=time.strftime('%Y-%m-%d_%H_%M_%S',time.localtime(time.time()))
f1 = open("C:/bz/"+"test_"+t+".txt",'w')
test=["1,2,3,4","5,6,7,8","9,10","11","abcd","qwer"]
for line in test:
    f1.write(line+"\n")
f1.close()
###读取数据
f1=open("C:/bz/"+"test_"+t+".txt",'r')
lines=f1.readlines()
for i in lines:
    print(i.split("\n")[0])##每行txt数据以 "\n" 结尾
f1.close()

3.爬取网页

import requests

###请求头
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '                            'Chrome/51.0.2704.63 Safari/537.36',
        }
#proxies={'https':'https://218.72.109.246:18118'}

url="https://www.sogou.com"
test_html = requests.get(url,headers=headers,timeout=5).text ##proxies=proxies
test_html=test_html.split("\n")
for line in range(len(test_html)):
    print(test_html[line])

4.BeautifulSoup

from urllib.request import urlopen
from bs4 import BeautifulSoup

url="https://www.douban.com/"

html = urlopen(url)
text_html = BeautifulSoup(html.read())
for i in text_html:
    print(i)

 

5.随机数

# 生成随机数
import random
print(random.uniform(10,20)) #float
print(random.randint(10,20)) #int
print(random.randrange(0,101,2)) #int step

# 选择随机数
import random
items = [18, 203, 5, 2, 7,8]
random.choice(items)

 

6.时间类

from datetime import datetime

date_str = "2028-5-10 17:53:59"
date_str1 = "2018-5-10 17:53:12"

startTime= datetime.strptime(date_str1,"%Y-%m-%d %H:%M:%S")
endTime= datetime.strptime(date_str,"%Y-%m-%d %H:%M:%S")

total_seconds = (endTime-startTime).total_seconds()
print(total_seconds)

seconds = (endTime-startTime).seconds
print(seconds)

days = (endTime-startTime).days
print(days)

import time
t=time.strftime('%Y-%m-%d_%H_%M_%S',time.localtime(time.time()))
print(t)
t=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print(t)

7.移除列表元素

# remove:只移除第一次出现的元素
a = [0, 2, 2, 3]
a.remove(2)
print(a) # [0,2,3]

# del:移除指定的位置的元素
a = [3, 2, 22, 1]
del a[2]
print(a) # [3,2,1]

#pop:与del移除类似,但是可以返回移除的元素
a = [3, 2, 22, 1]
b = a.pop(1)
print(b) # 2
print(a) # [3,22,1]

8.列表排序

# 1.数字排序
a = [12,2,1,-1,3,4,56,-98]
a.sort() #列表自带的sort排序,直接对原列表进行排序
print(a) # [-98, -1, 1, 2, 3, 4, 12, 56]

a = [12,2,1,-1,3,4,56,-98]
b=sorted(a) #内建函数sorted排序,不改变原列表
print(a) # [12, 2, 1, -1, 3, 4, 56, -98]
print(b) # [-98, -1, 1, 2, 3, 4, 12, 56]


# 2.字典排序:直接修改原list
items = [{'name': 'b', 'age': 7},
         {'name': 'a', 'age': 39},
         {"name": 'c', 'age': 21}]
items.sort(key=lambda item: item.get("age")) # 根据age升序
print(items)
# [{'name': 'b', 'age': 7}, {'name': 'c', 'age': 21}, {'name': 'a', 'age': 39}]

items.sort(key=lambda item: item.get("age"),reverse=True) # 根据age降序
print(items)
# [{'name': 'a', 'age': 39}, {'name': 'c', 'age': 21}, {'name': 'b', 'age': 7}]

items.sort(key=lambda item: item.get("name")) # 根据name排序
print(items)
# [{'name': 'a', 'age': 39}, {'name': 'b', 'age': 7}, {'name': 'c', 'age': 21}]

# 3.字典排序:不修改原list
items = [{'name': 'b', 'age': 7},
         {'name': 'a', 'age': 39},
         {"name": 'c', 'age': 21}]
new_items = sorted(items, key=lambda item: item.get("age"))
print(items)
# [{'name': 'b', 'age': 7}, {'name': 'a', 'age': 39}, {'name': 'c', 'age': 21}]
print(new_items)
# [{'name': 'b', 'age': 7}, {'name': 'c', 'age': 21}, {'name': 'a', 'age': 39}]

$ 4.二位列表排序
arr = [[10,16], [2,8], [1,6], [7,12]]
arr.sort(key = lambda x : x[1])
# [[1, 6], [2, 8], [7, 12], [10, 16]]

9.append&extend

# append表示把某个数据当做新元素追加到列表的最后面,它的参数可以是任意对象
x = [1, 2, 3]
y = [4, 5]
z = 6
x.append(y)
print(x) # [1, 2, 3, [4, 5]]
x.append(z)
print(x) # [1, 2, 3, [4, 5], 6]


# extend的参数必须是一个可迭代对象,表示把该对象里面的所有元素逐个地追加到列表的后面
x = [1, 2, 3]
y = [4,5]
z = 6
x.extend(y)
print(x) # [1, 2, 3, 4, 5]
#x.extend(z) #报错

 10.优雅访问列表下标索引

#初始化list 一行(for)
maxNumbers=4
data = [x for x in range(maxNumbers)]
print(data) #[0,1,2,3]

#初始化list 一行(for)
nums=[1,2,3]
data = [x for x in nums]
print(data)#[1,2,3]


# 普通实现
items = [21, 213, 145, 89]
for index in range(len(items)):
    # 先访问下标,在访问值
    print(index, ":", items[index])

for index in range(len(items)):
    # 先访问下标(从1开始),在访问值
    print(index+1, ":", items[index])

# 优雅实现
for index, item in enumerate(items):
    print(index, ":", item)

for index, item in enumerate(items,start=1):
    print(index, ":", item)

11.对字典按照value进行排序 

words_num = {}
words = ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is","day","a","a","b","z"]
for i in words:
    words_num[i] = words_num.get(i,0) + 1
# 先按照value(int)排序,然后再按照key(字符大小)排序
sort_word_num = sorted(words_num.items(),key=lambda d:(-d[1],d[0]))
print(sort_word_num)
#[('the', 4), ('is', 3), ('a', 2), ('day', 2), ('sunny', 2), ('b', 1), ('z', 1)]

12 自动备份mysql数据库脚本

# -*- coding: utf-8 -*-
import os,time,pymysql

user='user'
pwd='pwd'

def getDatabaseNames():
    conn = pymysql.connect("localhost", user, pwd, use_unicode=True, charset="utf8")
    cur = conn.cursor()
    cur.execute('show databases;')
    dbs = cur.fetchall()
    cur.close()
    conn.close()
    return dbs

#path trim一下然后创建
def mkdir(path):
    path = path.strip()
    path = path.rstrip("\\")
    isExists = os.path.exists(path)

    if not isExists:
        os.makedirs(path)
        return True
    else:
        return False


if __name__ == '__main__':
    timestr = time.strftime("%Y%m%d%H%M%S",time.localtime(time.time()))
    folder = "mysql_data_bak/"+timestr
    mkdir(folder)

    dbs = getDatabaseNames()
    print(dbs)
    for db in dbs:
        try:
            dbname = db[0]
            #排除自带的db
            if dbname=="mysql" or dbname=="performance_schema" or dbname=="information_schema" or dbname=="sys":
                continue
            #导出db
            cmd = "mysqldump -u%s -p%s %s > %s/%s.sql" % (user, pwd, dbname, folder, dbname)
            #导入db:mysql -u用户名 -p密码 数据库名 < 数据库名.sql
            print(cmd)
            os.system(cmd)
        except Exception as e:
            print(e)

13 字目和数字转换

# A:65 Z:65+25=90
# a:97 z:97+25=122
print(ord('z'))
print(chr(122))

#整数之间的进制转换
hex(16)     # 10进制转16进制
oct(8)      # 10进制转8进制
bin(8)      # 10进制转2进制

#字符串转整数
int('10')       # 字符串转换成10进制整数
int('10',16)    # 字符串转换成16进制整数
int('0x10',16)  # 字符串转换成16进制整数
int('10',8)     # 字符串转换成8进制整数
int('010',8)    # 字符串转换成8进制整数
int('10',2)     # 字符串转换成2进制整数

14 dict初始化的方法

a={}
a.get("a",0)+1

a={}
a.setdefault("a",[]).append(1)
print(a)#{'a': [1]}

15 tuple

values = [5,4,3,2,1]
labels = [1,1,2,2,3]

tuple_v = [(val, label) for val, label in zip(values, labels)]
tuple_v_sort = sorted(tuple_v,reverse=True)
#[(5, 1), (4, 1), (3, 2), (2, 2), (1, 3)]
print(tuple_v_sort)

16 与输入顺序保持一致的字典

import collections
d1={}
d1=collections.OrderedDict()  #将普通字典转换为有序字典
d1['a']='A'
d1['b']='B'
d1['c']='C'
d1['d']='D'
for k,v in d1.items():
    print k,v

17 堆

'''
heaqp模块提供了堆队列算法的实现,也称为优先级队列算法。
要创建堆,请使用初始化为[]的列表,或者可以通过函数heapify()将填充列表转换为堆。
提供以下功能:
heapq.heappush(堆,项目)
将值项推入堆中,保持堆不变。
heapq.heapify(x)
在线性时间内将列表x转换为堆。
heapq.heappop(堆)
弹出并返回堆中的最小项,保持堆不变。如果堆是空的,则引发IndexError。
'''
import heapq

#1 heappush生成堆+ heappop把堆从小到大pop出来
heap = []
data = [1,3,5,7,9,2,4,6,8,0]
for i in data:
    heapq.heappush(heap,i)
print(heap) #[0, 1, 2, 6, 3, 5, 4, 7, 8, 9]

while heap:
    heapq.heappop(heap)
print(heap) #[]

# 第二种
nums = [2, 3, 5, 1, 54, 23, 132]
heapq.heapify(nums)
print([heapq.heappop(nums) for _ in range(len(nums))])  # 堆排序结果
# out: [1, 2, 3, 5, 23, 54, 132]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值