-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from stfc/Netbox_Data_Uploader
Added csv_to_dict.py as the first step to uploading data to Netbox.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pandas as pd | ||
|
||
|
||
class CsvUtils: | ||
""" | ||
This class provides methods to read data from CSV files and allow the data to be easily read and used elsewhere. | ||
""" | ||
@staticmethod | ||
def csv_to_python(file_path: str) -> dict: | ||
""" | ||
This method reads data from csv files and writes them to a dictionary. | ||
:param file_path: The file path of the CSV file to be read from. | ||
:return: Returns the data from the csv as a dictionary. | ||
""" | ||
with pd.read_csv(file_path) as dataframe: | ||
dataframe = dataframe.to_dict(orient="list") | ||
return dataframe | ||
|
||
@staticmethod | ||
def separate_data(data: dict) -> list: | ||
""" | ||
This method reduces Pandas CSV to Dict conversion to individual dictionaries. | ||
:param data: The data from the CSV file | ||
:return: Returns a list of dictionaries which each represent a row of data from CSV. | ||
""" | ||
data_keys = list(data.keys()) | ||
len_rows = len(data[data_keys[0]]) - 1 | ||
dicts = [] | ||
for index in range(len_rows): | ||
new_dict = {} | ||
for key in data_keys: | ||
new_dict.update({key: data[key][index]}) | ||
dicts.append(new_dict) | ||
return dicts |