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
|
import pandas as pd
from datetime import datetime
import json
consume = pd.read_excel(r'交易日志.xls' , usecols = "C,E,F" , skipfooter = 1 , date_parser = ['交易时间'])
df_filtered = consume[consume['交易类型'] == '消费' ]
df = df_filtered[['交易时间','交易金额(元)']]
df['交易时间'] = pd.to_datetime(df['交易时间'])
# 2017与2021年需要补齐x轴
df_2017 = df[(df.loc[:,'交易时间'].dt.year == 2017)]
daysum_2017 = (df_2017.resample('D', on = '交易时间').sum())
idx = pd.date_range(start='2017-01-01 00:00:00', end='2017-12-31 00:00:00', freq='D')
daysum_2017 = daysum_2017.reindex(idx, fill_value=0)
daysum_2017 = daysum_2017.round(2)
daysum_2017.to_csv("2017.csv" , header = False)
df_2018 = df[(df.loc[:,'交易时间'].dt.year == 2018)]
daysum_2018 = (df_2018.resample('D', on = '交易时间').sum())
daysum_2018 = daysum_2018.round(2)
daysum_2018.to_csv("2018.csv" , header = False)
df_2019 = df[(df.loc[:,'交易时间'].dt.year == 2019)]
daysum_2019 = (df_2019.resample('D', on = '交易时间').sum())
daysum_2019 = daysum_2019.round(2)
daysum_2019.to_csv("2019.csv" , header = False)
df_2020 = df[(df.loc[:,'交易时间'].dt.year == 2020)]
daysum_2020 = (df_2020.resample('D', on = '交易时间').sum())
daysum_2020 = daysum_2020.round(2)
daysum_2020.to_csv("2020.csv" , header = False)
df_2021 = df[(df.loc[:,'交易时间'].dt.year == 2021)]
daysum_2021 = (df_2021.resample('D', on = '交易时间').sum())
idx = pd.date_range(start='2021-01-01 00:00:00', end='2021-12-31 00:00:00', freq='D')
daysum_2021 = daysum_2021.reindex(idx, fill_value=0)
daysum_2021 = daysum_2021.round(2)
daysum_2021.to_csv("2021.csv" , header = False)
# 转换成json格式
import os
import zipfile
def convert_to_json(year):
input = str(year)
with open(input+'.csv','r') as f:
fl = f.readlines()
result = []
for _ in fl:
a = list(_)
a = a[5:-1]
a.insert(5,'"')
a.insert(0,'"')
_ = ''.join(a)
_ = _.strip('\n')
result.append('['+f'{_}'+'],')
result[-1] = result[-1].strip(',')
fw = open(input+".txt","w")
fw.writelines('[')
fw.writelines(result)
fw.writelines(']')
fw.close()
# 在服务器上打包
if __name__=="__main__":
for _ in range(2017,2022):
convert_to_json(_)
file = r'test.zip'
out_path = os.getcwd()
zip_1 = zipfile.ZipFile(file, 'w')
for f in os.listdir(out_path):
if f.endswith('.txt'):
zip_1.write(os.path.join(out_path, f), f, zipfile.ZIP_DEFLATED)
zip_1.close()
|