-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnb_module.py
More file actions
84 lines (56 loc) · 2.22 KB
/
snb_module.py
File metadata and controls
84 lines (56 loc) · 2.22 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
import csv
def series_id_map(list1, list2):
'''(list1, list2)->dict
This function maps the values of a list to the values of another list and stores them in a dictionary
that the function then returns.
Example:
>>>series_id_map(titles,series_ids)
series_dict = {'Example Title': 'exsrsid00', 'Another Title': 'anthrsid01', etc ...}
'''
def create_data_files(series_file):
'''(single csv file)->(multiple data files)
This function takes a horizontally oriented csv file with multiple data columns and
creates an equal number of time-series data files.
Example:
>>>create_data_files('example.csv')
Created exsrsid00, anthrsid01.
'''
# Open csv file for reading
csv_file = open(series_file, 'r')
csv_reader = csv.reader(csv_file)
# Declare variables of column locations in order to loop through each row and column to map dates with observations.
date_col_num = 0
series_col_num = 1
# Count the number of columns in the CSV file and then return to the beginning of the file.
ncol = len(next(csv_reader))
series_count = ncol - 1
csv_file.seek(0)
print('There are ' + str(series_count) + ' series.' + "\n")
# Loop through each series id to print observations to the file.
while series_col_num < ncol:
current_row = 0
# First, prints header with 'Date' and series id on the first line of the file; then prints each date and matching observation from every row on successive lines.
for row in csv_reader:
if current_row == 0:
header = row
f = open(header[series_col_num], 'w')
f.write(header[date_col_num] + "\t" + header[series_col_num] + "\n")
else:
f.write(row[date_col_num] + "\t" + row[series_col_num] + "\n")
current_row += 1
# Close the series file
f.close()
# Return to the beginning of the CSV file in order to create all series data files.
csv_file.seek(0)
# Move to next column that contains data
series_col_num += 1
# Close the CSV file
csv_file.close()
# def count_series(time_series_csv):
# '''(csv file)->int
# Counts the columns in the csv file to determine the amount of series that should be processed. Function returns an integer that represents
# the amount of series.
# '''
# ncol = len(next(time_series_csv))
# csv_file.seek(0)
# return ncol