1. Create a new .py file, modify the file storage path, and run it:
#csv文件批量转化为hdf文件
import os
import pandas as pd
def csv_save_as_hdf(data_path):
for dirpath,dirname,filenames in os.walk(data_path): #os.walk()遍历文件
for fname in filenames:
file_name=os.path.join(dirpath,fname) #os.path.join()合并路径
if file_name.endswith('.csv'):
df=pd.read_csv(file_name)
floder,name=os.path.splitext(file_name) #分割后缀
df.to_hdf(f"{floder}.h5",file_name)
print(f'{file_name} 转换成功啦!)
print('-----------------------------------------------------------------------------------------------------------')
os.remove(file_name)
print('finished...')
data_path=r"D:\wangxiaoying\shujuji\data" #csv文件所存的位置
csv_save_as_hdf(data_path) #调用一下,转换完成
2. The conversion is successful~~