-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManagement.py
More file actions
96 lines (85 loc) · 2 KB
/
Copy pathDataManagement.py
File metadata and controls
96 lines (85 loc) · 2 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
class DataMng():
"""this class contain functions for data management"""
def formatChk(fp):
"""
this function is checking the format of file and pass the format as string.
"""
if(type(fp)==str):
stForm=''
form = fp.split('.')
stForm = form[1]
return stForm
def csvRead(fp):
"""
this function is reading the csv and passing the string content of it.
"""
data=''
frm = DataMng.formatChk(fp)
if(frm == 'csv'):
try:
with open(fp) as f:
data = f.read()
f.close()
pass
except:
print('there is a problem to read the file!')
return data
def csvRw(fp):
"""
this function returning each row of csv in a list.
(separating each lines of the csv file)
"""
dataArr=[]
frm = DataMng.formatChk(fp)
if(frm == 'csv'):
data = DataMng.csvRead(fp)
try:
dataArr = data.splitlines()
except:
print('there is problem to spliting lines!')
else:
print('make sure about your file format!')
return dataArr
def csvIndx(fp):
"""
this function returns the list of columns in the csv file.
"""
indx=[]
frm = DataMng.formatChk(fp)
if(frm == 'csv'):
dataArr = DataMng.csvRw(fp)
try:
indx = dataArr[0].split(',')
pass
except:
print('there is a problem to split lines!')
else:
print('make sure about your file format!')
pass
return indx
pass
def csvDic(fp):
"""
this function converting the csv to dictionary
"""
frm = DataMng.formatChk(fp)
dic = {}
dataRw=[]
if(frm=='csv'):
dataArr = DataMng.csvRw(fp)
nIndx =len(DataMng.csvIndx(fp))
n=0
for item in dataArr:
rw = item.split(',')
dataRw.append(rw)
n = len(dataRw)
dataRw_flMtx = [[dataRw[j][i] for j in range(n)] for i in range(len(dataRw[0]))]
#print(dataRw_flMtx[4][1:])
for i in range(nIndx):
for j in range(1,n):
d = dict(zip(dataRw_flMtx[i][:1],dataRw_flMtx[i][1:]))
dic.update(d)
pass
return dic
fp = 'D:\Arman\Iaac\Y2\Python\GH_Data_Visualization\AdultFormatted.csv'
print(DataMng.csvDic(fp))