-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_processing.py
96 lines (78 loc) · 3.09 KB
/
csv_processing.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
import logging
import csv
import sys
import os
module_logger = logging.getLogger("Perf_reporter.csv_processing")
def csv_file_name(template_path):
logger = logging.getLogger("Perf_reporter.csv_processing.csv_file_name")
csv_report_path = 'report_files/' + str(os.path.splitext(template_path)[0].split('/')[1]) + '_report' + os.path.splitext(template_path)[1]
logger.debug(os.path.splitext(template_path)[0].split('/')[1])
logger.debug(os.path.splitext(template_path)[1])
return csv_report_path
def csv_read(read_path):
query_list = []
"""
Read a csv file
"""
try:
with open(read_path, "r") as in_file:
reader = csv.reader(in_file, delimiter=';')
# print(list(reader))
for row in reader:
query_list.append(row)
# deleting last line which is reserved for target_services querry
del query_list[-1]
return query_list
except OSError as e:
print(e)
sys.exit("Error reading template, check file path")
def csv_read_last_line(read_path):
try:
with open(read_path, "r") as in_file:
last_line = in_file.readlines()[-1].split(";")
# print("__________________________________________LAST_LINE_______________________________________________")
# print(last_line)
return last_line
except OSError as e:
print(e)
sys.exit("Error reading template, check file path")
def csv_read_first_line(read_path):
try:
with open(read_path, "r") as in_file:
first_line = in_file.readlines()[0].split(";")
# print("__________________________________________FIRST_LINE______________________________________________")
# print(last_line)
return first_line
except OSError as e:
print(e)
sys.exit("Error reading template, check file path")
def csv_write(data, write_path):
"""
Пишем в CSV
"""
try:
with open(write_path, "w", newline='') as out_file:
writer = csv.writer(out_file, delimiter=';')
for row in data:
# print('write')
# print(line)
writer.writerow(row)
logger = logging.getLogger("Perf_reporter.csv_processing.csv_write")
logger.info('Отчет в формате csv создан: ' + write_path)
except OSError as e:
print(e)
def csv_add(data, start_index, end_index, write_path):
"""
Пишем в CSV
"""
try:
with open(write_path, "a", newline='') as out_file:
writer = csv.writer(out_file, delimiter=';')
for row in range(start_index, end_index):
# print('write')
# print(line)
writer.writerow(data[row])
logger = logging.getLogger("Perf_reporter.csv_processing.csv_add")
logger.info('Added to: ' + write_path)
except OSError as e:
print(e)