-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_file.py
75 lines (55 loc) · 2.01 KB
/
read_file.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Contact email: dimitris.rizopoulos@inlecomsystems.com
import os
import numpy as np
import pickle5 as pickle
from openpyxl import load_workbook
script_dir = os.path.dirname(__file__)
def read_pickle(file_path):
result = []
if os.path.isfile(file_path):
with open(file_path, 'rb') as handle:
result = pickle.load(handle)
return result
def read_xlsx(file_path):
workbook_1 = load_workbook(file_path)
worksheets_1 = workbook_1.sheetnames
# N is the number of rows in the input sheet
N_1 = 0
M_1 = 0
for row in workbook_1[worksheets_1[0]]:
N_1 = N_1 + 1
if (N_1 == 1):
for column in row:
M_1 = M_1 + 1
result = np.empty((N_1 - 1, M_1), dtype=object)
for row in range(1, N_1):
for column in range(M_1):
row_1 = row - 1
result[row_1][column] = workbook_1[worksheets_1[0]].cell(row=row + 1, column=column + 1).value
return result
def dump_xlsx_to_pickle(xlsx_path, pickle_path):
result = read_xlsx(xlsx_path)
with open(pickle_path, 'wb') as handle:
pickle.dump(result, handle, protocol=pickle.HIGHEST_PROTOCOL)
def dump_table_to_pickle(table, pickle_path):
with open(pickle_path, 'wb') as handle:
pickle.dump(table, handle, protocol=pickle.HIGHEST_PROTOCOL)
def read_file(base_dir, input_file, create_pickle=True):
file_name = os.path.splitext(input_file)[0]
pickle_file_path = os.path.abspath(os.path.join(
base_dir,
file_name + '.pickle'))
xlsx_file_path = os.path.abspath(os.path.join(
base_dir,
file_name + '.xlsx'))
if os.path.exists(pickle_file_path):
result = read_pickle(pickle_file_path)
elif os.path.exists(xlsx_file_path):
result = read_xlsx(xlsx_file_path)
if create_pickle:
dump_xlsx_to_pickle(xlsx_file_path, pickle_file_path)
else:
raise Exception("File:{} does not exists".format(file_name))
return result