-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_output.py
100 lines (85 loc) · 3.33 KB
/
file_output.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
98
99
100
import datetime
from os import path
from text_detection_target import TextDetectionTargetWithResult
from sc_logging import logger
def save_text_files(results, out_folder, append_method_index):
for targetWithResult in results:
if targetWithResult.result is None:
continue
if (
"skip_empty" in targetWithResult.settings
and targetWithResult.settings["skip_empty"]
and len(targetWithResult.result) == 0
):
continue
if (
targetWithResult.result_state
!= TextDetectionTargetWithResult.ResultState.Success
):
continue
output_file_path = path.abspath(
path.join(
out_folder,
f"{targetWithResult.name}.txt",
)
)
append_method = "w"
if append_method_index == 1 or append_method_index == 2:
append_method = "a"
# check if the file exists, if it does, append the result to the file
try:
with open(output_file_path, append_method) as f:
f.write(f"{targetWithResult.result}")
if append_method == "a":
f.write("\n")
except Exception as e:
logger.error(f"Error writing to file: {e}")
def save_csv(results, out_folder, append_method_index, first_csv_append):
if not out_folder:
return
# add timestamp as first column
result_concat_for_aggreagate = f"{datetime.datetime.now().isoformat()},"
header = "Timestamp,"
for targetWithResult in results:
header += f"{targetWithResult.name},"
if (
targetWithResult.result_state
!= TextDetectionTargetWithResult.ResultState.Success
):
result_concat_for_aggreagate += ","
else:
result_concat_for_aggreagate += (
f"{targetWithResult.result}," if targetWithResult.result else ","
)
aggregate_output_file_path = path.abspath(path.join(out_folder, "results.csv"))
append_method = "w"
if append_method_index == 0 or append_method_index == 2:
append_method = "a"
try:
with open(aggregate_output_file_path, append_method) as f:
# if this is the first-time-appending or truncating, add the header
if first_csv_append or append_method == "w":
f.write(header + "\n")
first_csv_append = False
f.write(result_concat_for_aggreagate + "\n")
except Exception as e:
logger.error(f"Error writing to aggregate file: {e}")
def save_xml(results, out_folder):
if not out_folder:
return
aggregate_output_file_path = path.abspath(path.join(out_folder, "results.xml"))
try:
with open(aggregate_output_file_path, "w") as f:
# add xml preamble
f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
f.write("<data>")
for targetWithResult in results:
# serialize dictionary to xml
dictionary = targetWithResult.to_dict()
f.write("<result>")
for key in dictionary:
f.write(f"<{key}>{dictionary[key]}</{key}>")
f.write("</result>")
f.write("</data>")
except Exception as e:
logger.error(f"Error writing to xml file: {e}")