Hi there, I am learning python and I am having trouble outputting the data from multiple csv files into a tree structure which would give me a branch for each csv file in a directory.
Then I can use gh components to split the branches lists and organize them to construct points (this part I am able to do no problem).
the python script i have been able to put together does the following:
-grabs all the csv files in a directory
-sorts them into a dictionary
I understand that in I can only output items or lists. But I need a tree-like structure and havent been able to get it.
I have attached two csv files and the ghp component I put together. In the script I am only selecting one ‘key’ (line 26,27) but the goal is to have maybe a for loop to read all values of the dictionary as branches. thanks in advance for any comments or direction in the right path
csvFiles.zip (574 Bytes)
2023-09-12_csvreadfilesasbranches.gh (9.0 KB)
import rhinoscriptsyntax as rs
import csv
import os
import glob
def read_csv_to_list(file_path):
data = []
with open(file_path) as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
data.append(row)
return data
directory_path = str(directory_path)
csv_files = glob.glob(os.path.join(directory_path, '*.csv'))
data_dict = {}
for csv_file in csv_files:
file_name = os.path.splitext(os.path.basename(csv_file))[0]
data = read_csv_to_list(csv_file)
data_dict[file_name] = data
flattened_list = [item for sublist in data_dict.values() for item in sublist]
print(flattened_list)
values = flattened_list[1]
surveysList = data_dict