-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_years.py
More file actions
108 lines (78 loc) · 3.6 KB
/
data_years.py
File metadata and controls
108 lines (78 loc) · 3.6 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
import csv
import os
def name_for_file(first_part: str, second_part: str) -> str:
"""Creates a name for file
Args:
first_part (str): First part of future file's name
second_part (str): Second part of future file's name
Returns:
str: Name in special format
"""
f_p = first_part.replace('-', '') + '_'
s_p = second_part.replace('-', '') + '.csv'
return os.path.join('data_to_years_output', f_p + s_p)
def get_year_from_data(data: list[list[str]], index: int) -> int:
"""Gets the year from csv file
Args:
data (list[list[str]]): A list with dates and data
index (int): The index of the string that points on list in the list
Returns:
int: Value of year
"""
date = data[index][0]
year = ''
for numbers in date:
if numbers != '-':
year += numbers
else:
break
return int(year)
def data_to_years(file_name: str, output_directory: str) -> None:
"""Function that sorts data to different files where each individual file will correspond to one year
Args:
file_name (str): Path to file
Raises:
TypeError: No such file exists
"""
if os.path.exists(file_name):
if not os.path.exists(os.path.join(output_directory, 'data_to_years_output')):
os.mkdir(os.path.join(output_directory, 'data_to_years_output'))
with open(file_name, 'r', encoding='utf-8') as csvfile:
reader_object = list(csv.reader(csvfile, delimiter=","))
output = []
first_part_of_name = ''
second_part_of_name = ''
is_first = True
current_year = get_year_from_data(reader_object, 0)
last_year = get_year_from_data(
reader_object, len(reader_object) - 1)
for i in range(len(reader_object)):
if current_year < last_year:
if get_year_from_data(reader_object, i) == current_year:
if is_first:
first_part_of_name = reader_object[i][0]
is_first = False
output.append(reader_object[i])
second_part_of_name = reader_object[i][0]
elif get_year_from_data(reader_object, i) != current_year:
with open(os.path.join(output_directory, name_for_file(first_part_of_name, second_part_of_name)), 'w', encoding='utf-8') as csv_file:
writer = csv.writer(
csv_file, lineterminator='\n')
current_year += get_year_from_data(
reader_object, i) - get_year_from_data(output, 0)
for j in output:
writer.writerow((j))
output = []
first_part_of_name = reader_object[i][0]
output.append(reader_object[i])
elif current_year == last_year:
output.append(reader_object[i])
second_part_of_name = reader_object[i][0]
if i + 1 == len(reader_object):
with open(os.path.join(output_directory, name_for_file(first_part_of_name, second_part_of_name)), 'w', encoding='utf-8') as csv_file:
writer = csv.writer(
csv_file, lineterminator='\n')
for j in output:
writer.writerow((j))
else:
raise FileNotFoundError