-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc_reader.py
More file actions
111 lines (76 loc) · 2.75 KB
/
Copy pathdoc_reader.py
File metadata and controls
111 lines (76 loc) · 2.75 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import logging
import re
import pandas
def time_it(func):
#"""Time messument Wrapper"""
import time
def inner():
start = time.time()
func()
end = time.time()
print("Execution time: {} sec.".format(end-start))
return inner
#def check_path(func):
#"""Input checks(path format) Wrapper for file_store function"""
#
# def inner(*args, **kwargs):
#
# return func if not re.findall(r'(\\)$', args) else logging.error("Format error. Backslash is needed in the end of path")
def paths_maker():
#"""Check files in folder and start json or html reader function"""
path = input()
data = os.listdir(path)
json_path = []
html_path = []
for i in data:
if len(i.split(".", 2))>2 and i.split(".", 2)[2] == "json":
json_path.append(path + "\\" + i)
elif i.split(".", 2)[1] == "html":
html_path.append(path + "\\" + i)
else:
logging.error("Extension error. This file is not json or html {}".format(i))
return json_path, html_path
def json_rw(json_path):
#"""Read json file("data" hash key) and give value"""
import json
json_path = json_path
json_data = []
for path in json_path:
with open(path) as json_file:
json_data.append((json.load(json_file), json_path))
json_data.append(('Data', 'Path'))
path = re.sub(r"[a-z]*$", "", json_path[-2])
path = path + "xlsx"
open(path, "w")
df = pd.DataFrame(json_data[:-1], index = None, columns = json_data[-1])
df.to_excel(path, sheet_name = 'json')
return logging.info("Excel successfully created {}.excel".format('json`s'))
def html_rw(html_path):
#"""Read html file and give value"""
html_paths = html_path
for html_path in html_paths:
html_data = []
df = pd.read_html(html_path , encoding = "utf-8")
for number in range(len(df[4]['Texts'])):
text = df[4]['Texts'][number]
time = df[4]['Duration (sec)'][number]
RTF = df[4]['RTF'][number]
WER = df[4]['WER%'][number]
html_data.append((text, time, RTF, WER))
html_data.append(('Text', 'Time', 'RTF', 'WER'))
path = re.sub(r"[a-z]*$", "", html_path)
path = path + "xlsx"
open(path, "w")
df_w = pd.DataFrame(html_data[:-1], index = None, columns = html_data[-1])
df_w.to_excel(path, sheet_name = 'html')
name = re.findall(r'\\([0-9A-Za-z_.-]*)\.[a-z]*$', path)
logging.info("Excel successfully created {}.excel".format(name[0]))
return logging.info("Excels successfully created ")
@time_it
def main():
json_path, html_path = paths_maker()
html_rw(html_path)
json_rw(json_path)
if __name__ == "__main__":
main()