-
Notifications
You must be signed in to change notification settings - Fork 0
/
EuclidDataTools.py
82 lines (72 loc) · 2.52 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
# -*- coding: utf-8 -*-
# @Time : 2023/3/11 10:45
# @Author : Euclid-Jie
# @File : EuclidDataTools.py
import pandas as pd
from pathlib import Path
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: Path = None
self.FullFolderPath: Path = None
def path_clear(self):
"""
get the full folder path and full file path
:return:
"""
if self.subFolder:
self.FullFolderPath = Path("./", self.subFolder)
self.FullFilePath = Path(self.subFolder, self.FileName)
else:
self.FullFolderPath = Path("./")
self.FullFilePath = Path("./", 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(overwrite)
:return:
"""
if not self.FullFilePath:
self.path_clear()
self.FullFolderPath.mkdir(parents=True, exist_ok=True)
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 Path(FullFilePath).exists():
# write after a exist file without header
df.to_csv(
FullFilePath, mode="a", encoding="utf_8_sig", header=False, index=False
)
else:
# write out a new file with header
df.to_csv(
FullFilePath, mode="w", encoding="utf_8_sig", header=True, 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)