HandleExcelMerge2021.py
6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import logging
import pandas as pdas
import os
import chardet
from djcardrenewal.common.dbase.MysqlHelper import MysqlHelper
from djcardrenewal.common.dbase import DbConect
from djcardrenewal.common.sqlcontent import DjSql
from djcardrenewal.common.sqlcontent import CurMonthYzl
from djcardrenewal.common.string.StringBuilder import StringBuilder
print("sys config " + __name__)
log_name = './log/cwrenewal/handlexcelmerge2021.log'
log_format = '[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]'
log_level = logging.DEBUG
log_mode = 'a'
logging.basicConfig(filename=log_name, format=log_format, level=log_level, filemode=log_mode)
class HandelCwExcelMergeMain:
handleDescription = ''
handleMonth = ''
hmonth = ''
db = None
def __init__(self, handle_month):
logging.info("init construct bean")
self.handleDescription = "dj renewal match iccid and device location"
self.handleMonth = handle_month
self.hmonth = handle_month.replace('-', '')[2:6]
self.db = MysqlHelper(DbConect.current_month_DB)
def initCurrentMonthTable(self):
logging.info("初始化:删除中间表")
# 判断文件编码
def detect_file_encoding(self, filename):
with open(filename, 'rb') as f:
content = f.read()
result = chardet.detect(content)
return result['encoding']
def dealDeviceEndDateList(self):
return True
# 定义修改函数
def modify_data(self, x):
# 进行批量修改的逻辑
if x is None:
return x
return x.replace("M{", "").replace("}", "")
# 定义修改函数
def modify_float_data(self, x):
# 进行批量修改的逻辑
if x is None:
return 0
current = str(x).replace("`", "").replace(' ', '')
if current == '':
return 0
return current
def exportDataToExcel(self):
# 指定目录路径
directory = 'D:/work_doc/000 - 审计/续费数据-商城续费/2021年电商/excelall/'
# 获取目录中的所有文件
files = os.listdir(directory)
# 读取 Excel 文件并合并数据
dataframes_121_01 = []
dataframes_121_02 = []
dataframes_124_01 = []
dataframes_124_02 = []
dataframes_208_01 = []
for file in files:
print("===文件名称:", file)
if (file.startswith('109All') or file.startswith('10014015_10037109All')
or file.startswith('10037109All') or file.startswith('1219139301All')
or file.startswith('1230909502All') or file.startswith('1232525202All')
or file.startswith('1244414702All') or file.startswith('1414496902All') or file.startswith('1251712801All')
or file.startswith('1466262802All') or file.startswith('1480056732All')
or file.startswith('1578954081All') or file.startswith('180000')
):
# file_path = os.path.join(directory, file)
file_path = directory + file
file_encode = self.detect_file_encoding(file_path)
df = pdas.read_csv(file_path, delimiter=',', encoding=file_encode)
dataframes_124_01.append(df)
if file.startswith('2088411221') :
# file_path = os.path.join(directory, file)
file_path = directory + file
file_encode = self.detect_file_encoding(file_path)
df = pdas.read_csv(file_path, delimiter=',', encoding=file_encode, skiprows=2)
dataframes_208_01.append(df)
# 合并所有数据
column_name = '应结订单金额'
merged_124_01_df = pdas.concat(dataframes_124_01, ignore_index=True)
merged_124_01_df = merged_124_01_df.dropna(subset=[column_name])
merged_124_01_df.to_excel('D:/work_doc/000 - 审计/续费数据-商城续费/2021年电商/excelout/1244414702All_data_2305.xlsx', index=False)
column_name = '入账时间'
merged_208_01_df = pdas.concat(dataframes_208_01, ignore_index=True)
merged_208_01_df = merged_208_01_df.dropna(subset=[column_name])
merged_208_01_df.to_excel('D:/work_doc/000 - 审计/续费数据-商城续费/2021年电商/excelout/2088411221_data_2305.xlsx', index=False)
def deleteEmptyData(self):
# 指定目录路径
directory = 'D:/work_doc/000 - 审计/续费数据-商城续费/2021年电商/excelout/'
# 获取目录中的所有文件
files = os.listdir(directory)
# 读取 Excel 文件并合并数据
dataframes_121_01 = []
dataframes_121_02 = []
dataframes_124_01 = []
dataframes_124_02 = []
dataframes_208_01 = []
for file in files:
print("===文件名称:", file)
if file.startswith('1244414702All') :
file_path = directory + file
dataframes_124_01 = pdas.read_excel(file_path)
if file.startswith('2088411221') :
file_path = directory + file
dataframes_208_01 = pdas.read_excel(file_path)
select_column = ['交易时间','微信订单号','商户订单号','应结订单金额','手续费']
dataframes_124_01_res = dataframes_124_01[select_column]
dataframes_124_01_res['收支类型'] = None
select_column = ['入账时间','支付宝交易号','业务订单号','收入(+元)','支出(-元)','账务类型']
dataframes_208_01_res = dataframes_208_01[select_column]
dataframes_208_01_res = dataframes_208_01_res.rename(columns={'入账时间': '交易时间', '支付宝交易号': '微信订单号', '业务订单号': '商户订单号', '收入(+元)': '应结订单金额', '支出(-元)': '手续费', '账务类型': '收支类型'})
# 批量修改某一列的数据
dataframes_208_01_res['商户订单号'] = dataframes_208_01_res['商户订单号'].apply(self.modify_data)
res_pd = pdas.concat([dataframes_124_01_res, dataframes_208_01_res], axis=0)
res_pd = res_pd[res_pd['应结订单金额'] != '应结订单金额']
res_pd['应结订单金额'] = res_pd['应结订单金额'].apply(self.modify_float_data).astype(float)
res_pd['手续费'] = res_pd['手续费'].apply(self.modify_float_data).astype(float)
res_pd.to_excel('D:/work_doc/000 - 审计/续费数据-商城续费/2021年电商/excelout/merge_2021All_data_2305.xlsx', index=False)
if __name__ == "__main__":
handle_month = '2023-05'
dedRes = False
diRes = False
urRes = False
#启动初始化
handleMain = HandelCwExcelMergeMain(handle_month)
logging.info("start handle" + handleMain.handleDescription + ' the month:' + handleMain.handleMonth)
# 整理数据
#handleMain.dealTheMonthData()
#将结果输出到excel
handleMain.exportDataToExcel()
# handleMain.deleteEmptyData()