• python常用工具


    读取和生成json文件

    from json import load,dumps
    
    
    # 读取json文件
    def read_json(file_path):
        file_json = None
        try:
            with open(file_path, mode='r', encoding="utf-8") as f:
                file_json = load(f)
        except Exception as e:
            print(f"read json error:{e}")
        finally:
            return file_json
    
    # 生成json文件
    def generate_json(content_dic,file_path):
        try:
            with open(file_path, "w", encoding="utf-8") as f:
                f.write(dumps(content_dic, indent=4, ensure_ascii=False))
        except Exception as e:
            print(f"generate json error:{e}")
    

    读取和生成csv文件

    import csv
    
    
    def read_csv(file_path):
        try:
            with open(file_path, "rb") as f:
                # 这里reader是一个生成器
                content = csv.reader(f)
                for row in content:
                    return row
        except Exception as e:
            print(f"read csv error:{e}")
    
    
    def generate_csv(file_path, content: tuple):
        try:
            with open(file_path, "wb",newline='',encoding='cp936') as f:
                # 使用python的csv生成excel所兼容的csv文件,设置参数dialect="excel"
                write = csv.writer(f, dialect="excel")
                write.writerow(content)
        except Exception as e:
            print(f"generate csv error:{e}")
    

    创建和读取exceL

    import xlwt
    import openpyxl as px
    
    # 创建excel,并创建sheet
    def creat_excel():
        # 新建workbook
        workbook = xlwt.Workbook()
        # 新建worksheet
        sheet = workbook.add_sheet("data")
    
        # 添加边框
        borders = xlwt.Borders()
        borders.left = xlwt.Borders.THIN
        borders.right = xlwt.Borders.THIN
        borders.top = xlwt.Borders.THIN
        borders.bottom = xlwt.Borders.THIN
        font = xlwt.Font()
        font.height = 20 * 11
        style = xlwt.XFStyle()
        style.borders = borders
        style.font = font
        return workbook, sheet, style
    
    # 创建excel,并创建sheet
    def creat_excel():
        book = px.Workbook()
        sheet = book.active
        return book, sheet
    
    
    # 主程序
    def generate_excel(content_list, output_file):
        book, sheet = creat_excel()
        try:
            sheet.append(["歌曲文件名", "歌词文件名"])
            for content in content_list:
                sheet.append(content)
            book.save(output_file)
        except Exception as e:
            logger.error(f"生成excel失败:{e}")
            book.close()
    
    
    
    def load_excel(excel_path):
        try:
            open_excel = px.load_workbook(excel_path)
            sheet = open_excel.worksheets[0]
            return sheet
        except Exception as e:
            print(f"{excel_path}:load_excel error:{e}")
    
    def read_excel(excel_path):
        sheet = load_excel(excel_path)
        for row in range(2, sheet.max_row + 1):
            data = sheet.cell(row, 1).value
    
    

    捕获异常

        try:
            main(csv_file, input_folder, result_folder)
            print("Finished...")
        except Exception as e:
            import traceback
    
            print(f"程序运行失败!!!请联系数据处理中心:{e}")
            print(traceback.format_exc())
        system("pause")
    

    获取视频时长

    def get_video_duration(filename):
        """
        获取视频文件时长
        """
        cap = cv2.VideoCapture(str(filename))
        if cap.isOpened():
            rate = cap.get(5)
            frame_num = cap.get(7)
            duration = frame_num / rate
            return duration
        return -1
    

    pydub

    form pydub import AudioSegment

    将音频读为一个对象进行处理

    • 音频读取
    audio_obj = AudioSegment.from_file('wavpath')
    
    • 音频时长获取

      • len(audio_obj) 结果为ms
      • audio_obj.duration_seconds
    • 音频声道数

      • L,R = wav.split_to_mono() 立体声抽取单声道
      • 合并单声道为立体声,也可合并多个单声道()
      AudioSegment.from_mono_audiosegments(1, 2, 3, 4) 
      
    • 音频参数修改

      • AudioSegment.from_file(str(src_file)).set_frame_rate(16000) 设置音频采样率(降采样)
      • set_sample_width 设置音频位宽
      • set_channels 设置通道数(仅支持单通道和双通道之间转换)
    • 音频标准化

    db = wav.max_dBFS
    normalized_sound = wav.apply_gain(-6.0-db) # 音频在-6db范围标准化
    
    • 音频拼接
    # 构建静音段
    silence = AudioSegment.silent(100, wav.frame_rate)
    # 实例化空白音频,并将静音段和正常的段拼接追加到空白音频中
    new_wav = silence + wav + silence
    
    • 音频截取
    st = int(pa['hdTimeStart'] * 1000)
    ed = int(pa['hdTimeEnd'] * 1000)
    new_wav = wav[st:ed]
    new_wav.export(path, format='wav')
    

    opencv读取保存包含中文路径的图片

    image_mask = cv2.imdecode(np.fromfile(src_jpg_image, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
    dst_gray_image = "output.png"
    cv2.imencode('.png', image_mask)[1].tofile(dst_gray_image)
    
    不论你在什么时候开始,重要的是开始之后就不要停止。 不论你在什么时候结束,重要的是结束之后就不要悔恨。
  • 相关阅读:
    docker pull 报X509错误
    Kong配置反向代理后获取原始IP
    MybatisPlus框架
    工厂模式
    Mybatis持久层框架
    linux 使用scp传输公钥时要注意事项
    docker 容器容器之间网络通信 docker-compose.yaml 配置固定ip
    Linux下执行sh文件提示权限不够解决办法
    docker-compose 编写yaml文件的注意事项
    nginx 中location url一定要带http://
  • 原文地址:https://www.cnblogs.com/yunhgu/p/15126746.html
Copyright © 2020-2023  润新知