-
Notifications
You must be signed in to change notification settings - Fork 3
/
fileops.py
54 lines (42 loc) · 1.44 KB
/
fileops.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
import os
import csv
import tools
import pandas as pd
from io import StringIO
from werkzeug import secure_filename
from xlrd import open_workbook
ALLOWED_EXTENSIONS = set(['xls', "xlsx"])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def save_file(file):
if file and allowed_file(file.filename):
random_folder = tools.id_generator()
filepath = "data/" + random_folder
os.makedirs(filepath)
filename = filepath + "/" + secure_filename(file.filename)
file.save(filename)
return filename
def clean_cell(cell):
output = cell.value.split("text:")[0].replace("'", "")
print(output)
return output
def read_excel(filename):
book = open_workbook(filename, on_demand=True)
output_list = []
for name in book.sheet_names():
output_dict = {}
sheet = book.sheet_by_name(name)
col_names = sheet.row(0)
for i in range(1, sheet.nrows, 1):
output_dict = {clean_cell(col_names[j]):
clean_cell(sheet.row(i)[j]) for j in range(0, len(col_names), 1)}
output_list.append(output_dict)
return output_list
def return_csv(data_array):
data_df = pd.DataFrame.from_dict(data_array)
output_io = StringIO()
data_df.to_csv(path_or_buf=output_io,
quoting=csv.QUOTE_ALL, index=False)
response_out = output_io.getvalue()
return response_out