功能
张表有两列字段,site和月份,现在需要根据这两个字段把csv拆成不同的csv,放到对应的文件夹中,比如site=4001/date=2017-01-01/下面只放对应数据的csv
文本格式 test_dome.csv
date,site
2017-01-01,4001
2017-01-01,4001
2017-01-01,4001
2017-01-01,4001
2017-01-01,4001
2017-01-01,4001
2017-01-01,4001
2017-01-01,4001
2017-01-01,4001
2017-01-01,5001
代码实现
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@File : ReadExcel.py
@Author: piepis
@Date : 2022/8/2
@Desc :
'''
if __name__ == '__main__':
input_filePath ="test_model.csv" #读取表路径
import pandas as pd
import os
data= pd.read_csv(input_filePath)
groups = data.groupby(['data', 'site'])
for group_key, group_value in groups:
group = groups.get_group(group_key)
print(group_key[0])
print(group_key[1])
date = str(group_key[0])
site = str(group_key[1])
file_path =os.path.join(date, site)
if os.path.exists(date):
if os.path.exists(os.path.join(date,site)):
file_name=os.path.join(file_path, "{0}_{1}.csv".format(date, site))
if os.path.exists(file_name):
os.remove(file_name)
group_value.to_csv(file_name, encoding="utf-8-sig", header=False, index=False)
print("{0}_{1}.csv have done".format(date, site))
else:
os.makedirs(os.path.join(date,site))
file_name=os.path.join(file_path, "{0}_{1}.csv".format(date, site))
group_value.to_csv(file_name, encoding="utf-8-sig", header=False, index=False)
print("{0}_{1}.csv have done".format(date, site))
else:
os.makedirs(date)
os.makedirs(os.path.join(date,site))
file_name = os.path.join(file_path, "{0}_{1}.csv".format(date, site))
group_value.to_csv(file_name, encoding="utf-8-sig", header=False, index=False)
print("{0}_{1}.csv have done".format(date, site))