用Python实现数据透视表、音频文件格式转换
1.用Python实现数据透视表
- import pandas as pd
-
-
- if __name__ == '__main__':
- # df = pd.read_excel('广告-资源位变现效率监测看板-1.xlsx', sheet_name='各业务在该资源位的明细数据')
- df = pd.read_excel('填充率分析-Q3.xlsx', sheet_name='库存底表')
- df = df.loc[df['dt'].str.startswith('2023-09-21')]
- # df = df.loc[df['dt'].str.startswith('2023-09-')]
- df = df.loc[df['资源位'] == '亮屏贴片']
- # df = df.loc[df['业务类型'] == '品牌广告']
- total = df['库存'].sum()
- print(total)
2.用Python实现音频文件格式转换
- from pydub import AudioSegment
- import os
-
-
- def add_path():
- path = os.environ.get('PATH')
- path = path[:-1]
- # new_path = 'C:\\Program Files\\gs\\gs10.01.2\\bin;.'
- new_path = 'C:\\myPC\\tools\\ffmpeg\\bin;.'
- updated_path = path + new_path
- os.environ['PATH'] = updated_path
-
-
- def convert_m4a_to_wav(input_file, output_file):
- audio = AudioSegment.from_file(input_file, format="m4a")
- audio.export(output_file, format="wav", parameters=["-ar", "16000"])
- print(f"Duration of {output_file}: {audio.duration_seconds} seconds")
- return audio.duration_seconds
-
-
- if __name__ == '__main__':
- add_path()
- directory = "./M4A/"
- files = os.listdir(directory)
- total_seconds = 0.0
- for file in files:
- print("Processing ", file)
- file = file.replace(".m4a", "")
- total_seconds = total_seconds + convert_m4a_to_wav("./M4A/" + file + ".m4a", "./WAV/" + file + ".WAV")
-
- total_minutes = total_seconds / 60.0
- total_hours = total_minutes / 60.0
- print("Total: " + str(total_seconds) + " sec; ")
- print("Total: " + str(total_minutes) + " min; ")
- print("Total: " + str(total_hours) + " hour; ")