forked from huaisha1224/jd-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel.py
45 lines (40 loc) · 1.48 KB
/
excel.py
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from time import strftime
import os
import csv
def save_to_csv(dealtime,number,vercode, amount,p_name):
"""
写入内容到csv
"""
dealtime = dealtime.split(' ')[0] # 切割时间取年月日
number = number + '\t'
vercode = vercode + '\t'
amount = amount[0]
data = (dealtime, number, vercode, amount, p_name)
file_path = strftime("save\\jd-assistant_%Y_%m.csv") # 按照当前系统时间创建文件
# 判断文件是否存在
if os.path.exists(file_path):
# 处理数据并写入
with open(file_path, 'a+', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(data)
else:
# 创建文件 处理数据并写入
path = os.path.dirname(os.getcwd() +'\\save\\') # 判断目录是否存在
if not os.path.exists(path):
os.makedirs(path)
header = ('下单时间', '订单号', '验证码', '实付金额', '商品名称')
with open(file_path, 'a+', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
with open(file_path, 'a+', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(data)
if __name__ == '__main__':
time = '2021-09-07 08:22:05'
number = '221697592258'
vercode = '7102298241802'
amount = ['248.00']
p_name = '培训体验课'
save_to_csv(time, number, vercode, amount, p_name)