Projekt

Obecné

Profil

Stáhnout (1.53 KB) Statistiky
| Větev: | Revize:
1 2d129043 petrh
import inspect
2
from Utilities.CSV import CSVDataLine
3
4 04a2b5a4 petrh
# Path to processed data
5 c8f3051b petrh
PROCESSED_DATA_PATH = "ProcessedData/"
6
7 04a2b5a4 petrh
8 c8f3051b petrh
def get_unique_names_from_file(filename, column_number):
9 04a2b5a4 petrh
    """
10 34cf65cd petrh
        Extract set of unique names from file
11 04a2b5a4 petrh
    Args:
12 34cf65cd petrh
        filename: path to processed file
13
        column_number: unique names are expected in csv file on column_number
14 04a2b5a4 petrh
15
    Returns:
16 34cf65cd petrh
        set of unique names
17 04a2b5a4 petrh
    """
18 c8f3051b petrh
    # create set of unique names
19
    name_set = set()
20
21 34cf65cd petrh
    with open(filename, "r") as file:
22
        # go through every line of line
23
        for x in file:
24
            # split by csv splitter ;
25
            array = x.split(";")
26
            # add string from chosen column to set
27
            name_set.add(array[column_number])
28 c8f3051b petrh
29
    return name_set
30
31
32
def export_data_to_csv(filename, data_dict):
33 34cf65cd petrh
    """
34
        Takes data_dict and export it into a csv file
35
    Args:
36
        filename: name of exported file
37
        data_dict: dictionary containing data from DatasetProcessor
38
    """
39 c8f3051b petrh
    with open(PROCESSED_DATA_PATH + filename[12:], "w+") as file:
40
41
        for date in data_dict:
42 2d129043 petrh
            if len(date) != 10:
43
                raise ValueError("Invalid date format for key value --> ddmmYYYYhh expected!")   
44 c8f3051b petrh
            for data in data_dict[date]:
45 2d129043 petrh
                csv_line = data_dict[date][data]
46
                if not isinstance(csv_line,CSVDataLine.CSVDataLine):
47
                    raise ValueError("data_dict is expected to have CSVDataLine as values")
48
                file.write(csv_line.to_csv() + '\n')