-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEuclidDataTools.py
88 lines (78 loc) · 2.85 KB
/
EuclidDataTools.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
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
# -*- coding: utf-8 -*-
# @Time : 2023/3/11 10:45
# @Author : Euclid-Jie
# @File : EuclidDataTools.py
import os
import pandas as pd
class EuclidCsvTools:
"""
this class include tools used to precess csv file
"""
def __init__(self, subFolder: str = None, FileName: str = 'DemoOut.csv'):
# para init
self.subFolder = subFolder
self.FileName = FileName
self.FullFilePath = None
self.FullFolderPath = None
def path_clear(self):
"""
get the full folder path and full file path
:return:
"""
if self.subFolder:
if '\\' in self.subFolder:
self.FullFolderPath = os.getcwd() + self.subFolder
else:
self.FullFolderPath = os.getcwd() + '\\' + self.subFolder
else:
self.FullFolderPath = os.getcwd()
self.FullFilePath = os.path.join(self.FullFolderPath, self.FileName)
print('文件将存储在: {}'.format(self.FullFilePath))
def saveCsvFile(self, df, append=False):
"""
save data to csv
:param df: pd.DataFrame
:param append: True(append save) or False(over write)
:return:
"""
if not self.FullFilePath:
self.path_clear()
if os.path.exists(self.FullFolderPath):
# folder path exit
if append:
self.writeDf2Csv(df, self.FullFilePath)
else:
df.to_csv(self.FullFilePath, encoding='utf_8_sig', index=False)
else:
# no dir exists then make one and save data to csv
os.mkdir(self.FullFolderPath)
if append:
self.writeDf2Csv(df, self.FullFilePath)
else:
df.to_csv(self.FullFilePath, encoding='utf_8_sig', index=False)
@classmethod
def writeDf2Csv(cls, df, FullFilePath):
if not os.path.exists(FullFilePath):
# write out a new file with header
df.to_csv(FullFilePath, mode='w', encoding='utf_8_sig', header=True, index=False)
else:
# write after a exist file without header
df.to_csv(FullFilePath, mode='a', encoding='utf_8_sig', header=False, index=False)
class CsvClient(EuclidCsvTools):
def __init__(self, subFolder: str = None, FileName: str = 'DemoOut.csv'):
"""
:param subFolder:
:param FileName:
"""
super().__init__(subFolder=subFolder, FileName=FileName)
if FileName[-4:] != '.csv':
self.FileName = self.FileName + '.csv'
self.path_clear()
def insert_one(self, data):
if isinstance(data, dict):
data = pd.DataFrame([data])
elif isinstance(data, pd.DataFrame):
pass
else:
raise TypeError("传入参数仅支出dict和pd.DataFrame")
self.saveCsvFile(df=data, append=True)