超好用Python小功能(持续更新中)

文章目录

        • 一、字符串操作小功能
          • 1、把数字转为千位数值类型
          • 2、检测字符串是不是纯数字
          • 3、python列表的交、并、差集
          • 4、对列表中字典中的字典排序
          • 5、python 求角度大小
          • 6、已知一个点,围绕某个点旋转,求旋转后的坐标
          • 7、对列表内元素前后项操作
          • 8、对列表内元素成对操作
          • 9、python 删除括号包括嵌套括号中的内容
          • 10、对多种日期格式做校验
          • 11、检查字符串是否含有中文
        • 二、正则表达式小功能
          • 1、找出字符在字符串中的位置
          • 2、匹配年月日格式
          • 3、多字符拆分
          • 3、去掉括号及括号中的内容
          • 4、替换特殊符号固定字符串
        • 三、关于os的操作
          • 1、打印出文件的绝对路径和文件名称
          • 2、获取文件夹下所有的文件
        • 四、pandas 操作小功能
          • 1、pandas 找出某个值,在索引行的位置
          • 2、根据索引号获取某列
          • 3、根据索引号获取某行
          • 4、逐行读取
          • 5、把数据的Nan值都替换为某个值
          • 6、把一列拆分为多列
          • 7、把多列和成一列
          • 8、把字典转为Dataframe
          • 9、把列表转为Dataframe
          • 10、删除全部为nan、含有nan的行和列
          • 11、指定哪列为索引
          • 12、循环合并df
        • 五、给表格添加颜色
        • 六、Python 使用argparse参数的传递
        • 七、用flask实现文件上传和下载
        • 八、用logging实现日志管理(强大的日志功能)

一、字符串操作小功能

1、把数字转为千位数值类型
    str2 = '-1234567898.71'
    result = "{:,}".format(float(str2))
    print(result)

结果

-1,234,567,898.71

2、检测字符串是不是纯数字
    str2 = '1234567898.71'
    str3 = str2.replace('.','')
    print(str3.isdigit())

运行结果:

True

3、python列表的交、并、差集

可以对字符串、列表、集合操作

l1 = ['1','2','3','4']
l2 = ['3','4','5','6']

# 交集
result1 = [i for i in l1 if i in l2]
result2 = list(set(l1).intersection(set(l2)))
print(result1)
print(result2)

# 并集
result3 = list(set(l1).union(set(l2)))
print(result3)

# 差集
# 元素在 l1 不在 l2
result4 = list(set(l1).difference(set(l2)))
print(result4)

[‘3’, ‘4’]
[‘3’, ‘4’]
[‘2’, ‘5’, ‘4’, ‘6’, ‘1’, ‘3’]
[‘2’, ‘1’]

4、对列表中字典中的字典排序
rows = [{'wo': 1, 'pos': {'top': 22}}, {'wo': 2, 'pos': {'top': 33}}]

def func(top):
    print(top['pos']['top'])
    return top['pos']['top']

rows.sort(key=func,reverse=False)
print(rows)

[{‘wo’: 2, ‘pos’: {‘top’: 33}}, {‘wo’: 1, ‘pos’: {‘top’: 22}}]

5、python 求角度大小
# 求A的角度大小
tantA = 1
A= math.atan(tantA)*180/(math.pi)
print(A)

45.0

6、已知一个点,围绕某个点旋转,求旋转后的坐标
import numpy as np
import math

# 绕pointx,pointy逆时针旋转
def Nrotate(angle, valuex, valuey, pointx, pointy):
    angle = math.radians(angle)
    valuex = np.array(valuex)
    valuey = np.array(valuey)
    nRotatex = (valuex - pointx) * math.cos(angle) - (valuey - pointy) * math.sin(angle) + pointx
    nRotatey = (valuex - pointx) * math.sin(angle) + (valuey - pointy) * math.cos(angle) + pointy
    return nRotatex, nRotatey

# 绕pointx,pointy顺时针旋转
def Srotate(angle, valuex, valuey, pointx, pointy):
    angle = math.radians(angle)
    valuex = np.array(valuex)
    valuey = np.array(valuey)
    sRotatex = (valuex - pointx) * math.cos(angle) + (valuey - pointy) * math.sin(angle) + pointx
    sRotatey = (valuey - pointy) * math.cos(angle) - (valuex - pointx) * math.sin(angle) + pointy
    return sRotatex, sRotatey

x1 = 5
y1 = 5
# 顺时针旋转90度,以原点(0,0)旋转
sPointx ,sPointy = Srotate(90,x1,y1,0,0)
print(sPointx,sPointy)

5.0 -5.0

7、对列表内元素前后项操作
# 把相加等于5的两个元素替换为a
array = [1,3,2,3,2,1]
print(array)
i =0
while i < len(array)-1:
    up = array[i]
    down = array[i+1]
    if up+down == 5:
        del array[i]
        del array[i]
        array.insert(i,'a')
    i += 1
print(array)
8、对列表内元素成对操作
list1 = ['a','b','c','d','e','f']

temp = 2
for index,i in enumerate(list1):
    if index % 2==0:
        print(index,temp)
        print(list1[index:temp])
        temp += 2

0 2
[‘a’, ‘b’]
2 4
[‘c’, ‘d’]
4 6
[‘e’, ‘f’]

9、python 删除括号包括嵌套括号中的内容
def delete_bracket(strs):
    # 删除括号中的内容
    print('原文:', strs)
    strs = strs.replace('(','(').replace(')',')')
    res = [0]*len(strs)
    flag =False
    cnt = 0
    for index,value in enumerate(strs):
        if value == '(':
            res[index] =1
            flag = True
            cnt +=1
        if flag:
            if value == ')':
                if cnt >1:
                    res[index] =1
                    cnt -= 1
                else:
                    res[index] =1
                    flag = False
                    cnt -=1
            else:
                res[index] =1
    res_strs = [strs[index] for index,i in enumerate(res) if i ==0]
    result = ''.join(res_strs)
    print('改正后:',result)
    return result

strs = 'da()da((dad)dd)c'
print(delete_bracket(strs))

原文: da()da((dad)dd)c
改正后: dadac

10、对多种日期格式做校验
import re
from datetime import datetime
def try_parsing_date(date):
	if 8<= len(date) <= 10:
		date = date.replace('.','-').replace('/','-')
		try:
			datetime.strptime(date,'%Y-%m-%d')
			return True
		except Exception as e:
			return False
			
def check_date(date):
	date = str(date)
	regex = '\d{4}[年]\d{1,2}[月]\d{1,2}|\d{4}[年]'
	result = re.match(regex,date)
	if result or try_parsing_date(date):
		return True
	else:
		return False

result = check_date('2020/3/4')
print(result)

True

11、检查字符串是否含有中文
def check_chinese(string):
	for i in str(string):
		if u'\u4e00' <= i <= u'\u9fff':
			return True
	return False

二、正则表达式小功能

1、找出字符在字符串中的位置
	#找出点在字符串中所有的位置
    str1 = '123.456.789.87'
    index = [m.start() for m in re.finditer('\.', str1)]
    print(index )

结果

[3, 7, 11]

2、匹配年月日格式

匹配出“2019年3月25日”这种年月日格式

    if re.search(u"\d{4}[年]\d{1,2}[月]\d{1,2}[日]", data):
        title_date = re.findall(u"\d{4}年\d{1,2}月\d{1,2}[日]", data)[0]
        print(title_date)

运行结果:

2019年3月25日

3、多字符拆分
import re
# 多字符串拆分,保留分隔符
sent = 'A1B2C3E4'
rex = r'([ABCDEF])'
result = re.split(rex,sent)
if result[0] == '':
    del result[0]
result.append('')
result = [''.join(i) for i in zip(result[0::2],result[1::2])]
print(result)

# 不保留分隔符
rex2 = r'[ABCDEF]'
result2 = re.split(rex2,sent)
if result2[0] == '':
    del result2[0]
print(result2)

运行结果:

[‘A1’, ‘B2’, ‘C3’, ‘E4’]
[‘1’, ‘2’, ‘3’, ‘4’]

3、去掉括号及括号中的内容
    s = "(断章)你站在桥上看风景(123),看风景的人在楼上看你。明月装饰了你的窗子,你装饰了别人的梦"
    a = re.sub(u"\\(.*?\\)|\\(.*?\\)|\\{.*?\\}|\\[.*?\\]", "", s)
    print(a)

运行结果:

你站在桥上看风景,看风景的人在楼上看你。明月装饰了你的窗子,你装饰了别人的梦

4、替换特殊符号固定字符串
    s = "1.你站在桥上看风景,2、看风景的人在楼上看你。三、明月装饰了你的窗子,你装饰了别人的梦"
    a = re.sub(u":|:|\.|1|2|三|、|", "", s)
    print(a)

你站在桥上看风景,看风景的人在楼上看你。明月装饰了你的窗子,你装饰了别人的梦

三、关于os的操作

1、打印出文件的绝对路径和文件名称
	# __file__,也可以是当前文件夹下的文件名称
    print(os.path.dirname(os.path.realpath(__file__)))
    print(os.path.basename(os.path.realpath(__file__)))

运行结果:

D:\code
test.py

2、获取文件夹下所有的文件
def get_file(dirpath):
    filenames = []
    for root, dirs, files in os.walk(dirpath):
        for file in files:
        	file_type = file.split('.')[-1]
            if file_type in ['pdf']:
                filenames.append(os.path.join(root, file))
    return filenames
file_paths = './'
files = get_file(file_paths)

四、pandas 操作小功能

1、pandas 找出某个值,在索引行的位置
   # 找出test在pandas所在的位置
   index = list(pd.index).index('test')
   # 列表索引是从0开始
   print(index+1)
2、根据索引号获取某列
   import numpy as np
   import pandas as pd
   #创建一个Dataframe
   df=pd.DataFrame(np.arange(16).reshape(4,4),index=list('abcd'),columns=list('ABCD'))
   
   # 第一种方法:
   # 根据索引0,获取第一列的列名
   col_name = df.columns[0]
   # 获取某列
   print(df[col_name])
   
   #第二种方法
   print(df.iloc[:,0])

A B C D
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
a 0
b 4
c 8
d 12
Name: A, dtype: int32
a 0
b 4
c 8
d 12
Name: A, dtype: int32

3、根据索引号获取某行
   # 第一种方法:
   # 根据索引0,获取第一行索引的名称
   index_name = df.index[0]
   # 获取某列
   print(df.loc['a'])

   #第二种方法
   print(df.iloc[0])

这里是引用
A B C D
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
A 0
B 1
C 2
D 3
Name: a, dtype: int32

4、逐行读取
   for row in df.values:
       print(row)
5、把数据的Nan值都替换为某个值
   # 把所有的nan用str代替
   df = df.fillna('str')
6、把一列拆分为多列
   # 把 col列以逗号拆分为多列
   new_df = df['col'].str.split(',').apply(pd.Series)
7、把多列和成一列
   # 把col和col1列以逗号合并
   new_df = df['col'].str.cat(df['col1'],sep=',')
8、把字典转为Dataframe
   # key作为列名转为df,key作为列索引,相当于子列表作为一列
   test = {'col':['a','b'],'col2':['c','d']}
   df= pd.DataFrame(test)
   print(df)

col col2
0 a c
1 b d

9、把列表转为Dataframe
   # 把子列表做为行的形式转为df
   test = [['a','b'],['c','d']]
   df= pd.DataFrame(test)
   print(df)

0 1
0 a b
1 c d

10、删除全部为nan、含有nan的行和列
# 删除表中全部为NaN的行
df.dropna(axis=0,how='all')
# 删除表中含有任何NaN的行
df.dropna(axis=0,how='any') #drop all rows that have any NaN values


# 删除表中全部为NaN的列
df.dropna(axis=1,how='all')
# 删除表中含有任何NaN的列
df.dropna(axis=1,how='any') #drop all rows that have any NaN values
11、指定哪列为索引
# 把0列设为索引,0位索引名称,不是索引号,并删除这一列
df.set_index(0,drop=True)
12、循环合并df
result_df = pd.DataFrame()
files = ['./1.xlsx', './2.xlsx', './3.xlsx']
for index,i in enumerate(files):
    print('==========={}_{}=========='.format(index,i))
    df = pd.read_csv(i)
    # axis=0 以行的形式合并; axis=1 以列的形式合并
    result_df = pd.concat([result_df,df],axis=0)
    
result_df.to_excel('WID_World.xlsx',index=False)

五、给表格添加颜色

import openpyxl
from openpyxl.styles import Font  # 导入字体模块
from openpyxl.styles import PatternFill  # 导入填充模块

wk = openpyxl.load_workbook("./table_dir/第二个表 .xlsx")  # 加载已经存在的excel
wk_name = wk.sheetnames
wk_sheet = wk[wk_name[0]]
fille = PatternFill('solid', fgColor='FFBB00')  # 设置填充颜色为 橙色

font = Font(u'宋体', size=11, bold=True, italic=True, strike=True, color='FFBB00')  # 设置字体样式

#添加颜色
wk_sheet['A2'].fill = fille  # 应用填充样式在A1单元格
# 给表格设置字体
wk_sheet.cell(row=2, column=2).font = font
# 赋值
wk_sheet.cell(row=2, column=2).value = '3'

wk.save(r"./table_dir/第一个表.xlsx")  # 保存excel

超好用Python小功能(持续更新中)_第1张图片

六、Python 使用argparse参数的传递

脚本test.py

import argparse

def str2bool(value):
    if value.lower() in ('yes', 'true', 'y', '1'):
        return True
    elif value.lower() in ('no', 'false', 'n','0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Unsupported value encountered!')

def main():
    # description描述这个参数解析器是干什么的,当我们在命令行显示帮助信息的时候会看到description描述的信息
    parser = argparse.ArgumentParser(description="parameter")
    # 通过对象的add_argument函数来增加参数。
    # '-n','--name'表示同一个参数,default参数表示在运行命令时若没有提供参数,程序会将此值当做参数值
    parser.add_argument('-f', '--file')
    parser.add_argument('--head', type=str2bool, default=False)
    args = parser.parse_args()
    file = args.file
    head = args.head
    print(file,head)

if __name__ == '__main__':
	main()

调用

python test.py -f D:\code\test.py -head False

七、用flask实现文件上传和下载

# -*- coding: utf-8 -*-
from werkzeug.utils import secure_filename
from flask import Flask,render_template,abort,request
from flask import send_from_directory
import os


app = Flask(__name__)
UPLOAD_FOLDER='upload'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
basedir = os.path.abspath(os.path.dirname(__file__))
ALLOWED_EXTENSIONS = set(['txt','png','jpg','xls','JPG','PNG','xlsx','gif','GIF'])

# 用于判断文件后缀
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS

# 用于测试上传
@app.route('/')
def upload_test():
    return render_template('base.html')

# 上传文件
@app.route('/upload',methods=['GET','POST'],strict_slashes=False)
def api_upload():
    if request.method == 'GET':
        return render_template('base.html')
    else:
        file_dir=os.path.join(basedir,app.config['UPLOAD_FOLDER'])
        if not os.path.exists(file_dir):
            os.makedirs(file_dir)
        f=request.files['file']  # 从表单的file字段获取文件,myfile为该表单的name值
        if f and allowed_file(f.filename):  # 判断是否是允许上传的文件类型
            file = f.filename
            f.save(os.path.join(file_dir,file))  #保存文件到upload目录
            return render_template('base.html')
        else:
            return render_template('base.html')
            # return jsonify({"errno":1001,"errmsg":"上传失败"})

@app.route('/download/', methods=['GET'])
def download(filename):
    if request.method =="GET":
        file_path =os.path.join(basedir,app.config['UPLOAD_FOLDER'])
        if os.path.join(file_path, filename):
            return send_from_directory(file_path,filename, as_attachment=True)
        abort(404)

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8067,debug=True)

项目见github

八、用logging实现日志管理(强大的日志功能)

在python代码中加统一的日志进行管理

import os
import logging
import logging.handlers

class logs(object):

    def __init__(self,logs_dir,logs_name,logger=None):
        '''
        指定保存日志的文件路径,日志级别,以及保存文件
        将日志存入指定的文件中
        :param logs_dir:
        :param logs_name:
        :param logger:
        logger = 1(详细日志信息)
        logger =2(运行日志信息)
        logger =3(统计日志信息)
        '''
        self.logs_dir = logs_dir
        self.logs_name = logs_name
        self.logger = logging.getLogger(logger)
        self.logger.setLevel(logging.INFO)
        if os.path.exists(logs_dir) and os.path.isdir(logs_dir):
            pass
        else:
            os.makedirs(logs_dir)

        self.log_path = os.path.join(logs_dir,logs_name)
        self.logs_name = self.log_path + '.log'
        fh = logging.FileHandler(self.logs_name,'a',encoding='utf-8')
        fh.setLevel(logging.DEBUG)

        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)

        if not self.logger.handlers:
            formatter = logging.Formatter('[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s]%(message)s')
            fh.setFormatter(formatter)
            ch.setFormatter(formatter)

            self.logger.addHandler(fh)
            self.logger.addHandler(ch)

            fh.close()
            ch.close()

    def getlog(self):
        return self.logger

if __name__ == '__main__':
    logger = logs(os.getcwd(), 'log','1').getlog()
    logger.info('提示信息')
    logger.warning('警告信息')

运行结果:

[2020-04-13 11:53:14,932] logs.py-> line:51 [INFO]提示信息
[2020-04-13 11:53:14,932] logs.py-> line:52 [WARNING]警告信息

你可能感兴趣的:(python)