-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
97 lines (76 loc) · 3.23 KB
/
dataset.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
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import csv
import pandas as pd
import json
import io
import os
from response import Response
class Dataset(Response):
def __init__(self, docUrl):
self.docUrl = docUrl
def loadData(self):
extension = os.path.splitext(self.docUrl)[1]
response = Response(self.docUrl).assertResponse()
if extension == "":
extension = response.headers.get("Content-Type")
if extension == ".ods":
xls = pd.ExcelFile(io.BytesIO(response.content))
print(f"Sheets in this file: {xls.sheet_names}")
out = {}
for sheet in xls.sheet_names:
out[sheet] = pd.read_excel(xls, sheet, engine="odf")
return out
elif extension == ".csv" or "csv" in extension:
with io.StringIO(response.text) as f:
dat = csv.DictReader(f)
colNames = dat.fieldnames
print(f"columns: {colNames}")
print("Converting the response into json format")
content = [{col.replace(" ", "_").lower() : row[col] for col in colNames} for row in dat]
return content
elif extension == ".xlsx" or extension == ".xls":
xls = pd.ExcelFile(io.BytesIO(response.content))
out = {}
for sheet in xls.sheet_names:
out[sheet] = pd.read_excel(xls, sheet)
return out
elif extension == ".json" or "json" in extension:
responseDict = response.json()
return responseDict
else:
return None
class WriteFile:
def __init__(self, basePath, dataToWrite, fileName, extension):
self.basePath = basePath
self.dataToWrite = dataToWrite
self.fileName = fileName
self.extension = extension
def writeFileToDisk(self):
#basePath = "data"
if not os.path.exists(self.basePath):
os.mkdir(self.basePath)
if not "." in self.extension:
extension = f".{self.extension}"
filePath = os.path.join(self.basePath, f"{self.fileName}{extension}")
print(f"\nWriting the file at {(os.path.abspath(filePath))}....")
for file in os.listdir(self.basePath):
if file == f"{self.fileName}{extension}":
os.remove(filePath)
if "json" in self.extension:
try:
#'if isinstance(self.dataToWrite, list or dict):
with open(filePath, "x") as f:
f.write(json.dumps(self.dataToWrite))
#print(f"written at {filePath}")
except:
raise TypeError(f"The file to write is of type {type(self.dataToWrite)}.\nTherefore, cannot be saved as a json file")
if "csv" in self.extension:
try:
#if "dataframe" in str(type(self.dataToWrite)).lower():
self.dataToWrite.to_csv(filePath, index=False)
except:
raise TypeError(f"The file to write is of type {type(self.dataToWrite)}.\nTherefore, cannot be saved as a csv file")
print(f"The file has been written")
return None