Icon

kn_​example_​python_​pickle_​dictionary

KNIME/Python - write and read back a dictionary

KNIME/Python - write and read back a dictionary


Pickle any Python object you want, save it as a ZIP file with the model wriiter node and read it back



save a dictionary in Python to disc and reload it laterhttps://forum.knime.com/t/output-a-dictionary-in-python-snippet/13543/4?u=mlauber71 import knime.scripting.io as knioimport numpy as npimport pandas as pdfrom pandas import DataFrameimport pickle# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_dict.html# https://stackoverflow.com/questions/26716616/convert-a-pandas-dataframe-to-a-dictionary/26716774a = {'a': ['red', 'yellow', 'blue'], 'b': [0.5, 0.25, 0.125]}pkl_file = knio.flow_variables['context.workflow.data-path'] + 'my_dictionary.pickle' with open(pkl_file, 'wb') as handle: pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)output_table = pd.DataFrame.from_dict(a, orient='columns')output_table = output_table.set_index(output_table.index.astype(str) + '_i1')knio.output_tables[0] = knio.Table.from_pandas(output_table) import knime.scripting.io as knioimport numpy as npimport pandas as pdfrom pandas import DataFrameimport pickle# reimport the listfrom dataframeinput_table = knio.input_tables[0].to_pandas()a = input_table.to_dict('list')pkl_file = knio.flow_variables['context.workflow.data-path'] + 'my_dictionary.pickle' with open(pkl_file, 'rb') as handle: b = pickle.load(handle)# c = pd.DataFrame.from_dict(b, orient='index')print(a == b)output_table = pd.DataFrame.from_dict(b, orient='columns')output_table = output_table.set_index(output_table.index.astype(str) + '_i2')knio.output_tables[0] = knio.Table.from_pandas(output_table) Pickle any Python object you want, save it as a ZIP file with the model writer node and read it back import knime.scripting.io as knioimport pandas as pdmy_dictionary = {'a': ['red', 'yellow', 'blue'], 'b': [0.5, 0.25, 0.125]}output_table = pd.DataFrame.from_dict(my_dictionary, orient='columns')output_table = output_table.set_index(output_table.index.astype(str) + '_i3')# These are the node's outputs that need to be populated:knio.output_tables[0] = knio.Table.from_pandas(output_table)knio.output_objects[0] = my_dictionary import knime.scripting.io as knioimport pandas as pdmy_dictionary_imported = knio.input_objects[0]output_table = pd.DataFrame.from_dict(my_dictionary_imported, orient='columns')# These are the node's outputs that need to be populated:output_table = output_table.set_index(output_table.index.astype(str) + '_i4')# output_table = output_table.reset_index(drop=True)knio.output_tables[0] = knio.Table.from_pandas(output_table) import knime.scripting.io as knio# bring the new style of an import object to the tranditional syntaxinput_object = knio.input_objects[0] import json# set the path for the pickel filepath = knio.flow_variables['context.workflow.data-path'] + 'my_dictionary.json'# Write the dictionary to a JSON filewith open(path, "w") as f: json.dump(input_object, f) import knime.scripting.io as knioimport json# set the path for the pickel filepath = knio.flow_variables['context.workflow.data-path'] + 'my_dictionary.json'with open(path , "r") as f: v_variable_list = json.load(f) knio.output_objects[0] = v_variable_list import knime.scripting.io as knioimport pandas as pdmy_dictionary_imported = knio.input_objects[0]output_table = pd.DataFrame.from_dict(my_dictionary_imported, orient='columns')# These are the node's outputs that need to be populated:output_table = output_table.set_index(output_table.index.astype(str) + '_i5')# output_table = output_table.reset_index(drop=True)knio.output_tables[0] = knio.Table.from_pandas(output_table) locate and create/data/ folderwith absolute pathssee if the results matchwrite dictionarymy_dictionary.zipPath variable created forvar_model_pathmy_dictionary.zipread dictionarysee if the results matchwrite dictionaryread dictionarymy_dictionary.jsonmy_dictionary.jsonsee if the results matchread dictionary Collect LocalMetadata Concatenate Python Script Model Writer Model Reader Python Script Concatenate Python Script Python Script Python Script Python Script Concatenate Python Script save a dictionary in Python to disc and reload it laterhttps://forum.knime.com/t/output-a-dictionary-in-python-snippet/13543/4?u=mlauber71 import knime.scripting.io as knioimport numpy as npimport pandas as pdfrom pandas import DataFrameimport pickle# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_dict.html# https://stackoverflow.com/questions/26716616/convert-a-pandas-dataframe-to-a-dictionary/26716774a = {'a': ['red', 'yellow', 'blue'], 'b': [0.5, 0.25, 0.125]}pkl_file = knio.flow_variables['context.workflow.data-path'] + 'my_dictionary.pickle' with open(pkl_file, 'wb') as handle: pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)output_table = pd.DataFrame.from_dict(a, orient='columns')output_table = output_table.set_index(output_table.index.astype(str) + '_i1')knio.output_tables[0] = knio.Table.from_pandas(output_table) import knime.scripting.io as knioimport numpy as npimport pandas as pdfrom pandas import DataFrameimport pickle# reimport the listfrom dataframeinput_table = knio.input_tables[0].to_pandas()a = input_table.to_dict('list')pkl_file = knio.flow_variables['context.workflow.data-path'] + 'my_dictionary.pickle' with open(pkl_file, 'rb') as handle: b = pickle.load(handle)# c = pd.DataFrame.from_dict(b, orient='index')print(a == b)output_table = pd.DataFrame.from_dict(b, orient='columns')output_table = output_table.set_index(output_table.index.astype(str) + '_i2')knio.output_tables[0] = knio.Table.from_pandas(output_table) Pickle any Python object you want, save it as a ZIP file with the model writer node and read it back import knime.scripting.io as knioimport pandas as pdmy_dictionary = {'a': ['red', 'yellow', 'blue'], 'b': [0.5, 0.25, 0.125]}output_table = pd.DataFrame.from_dict(my_dictionary, orient='columns')output_table = output_table.set_index(output_table.index.astype(str) + '_i3')# These are the node's outputs that need to be populated:knio.output_tables[0] = knio.Table.from_pandas(output_table)knio.output_objects[0] = my_dictionary import knime.scripting.io as knioimport pandas as pdmy_dictionary_imported = knio.input_objects[0]output_table = pd.DataFrame.from_dict(my_dictionary_imported, orient='columns')# These are the node's outputs that need to be populated:output_table = output_table.set_index(output_table.index.astype(str) + '_i4')# output_table = output_table.reset_index(drop=True)knio.output_tables[0] = knio.Table.from_pandas(output_table) import knime.scripting.io as knio# bring the new style of an import object to the tranditional syntaxinput_object = knio.input_objects[0] import json# set the path for the pickel filepath = knio.flow_variables['context.workflow.data-path'] + 'my_dictionary.json'# Write the dictionary to a JSON filewith open(path, "w") as f: json.dump(input_object, f) import knime.scripting.io as knioimport json# set the path for the pickel filepath = knio.flow_variables['context.workflow.data-path'] + 'my_dictionary.json'with open(path , "r") as f: v_variable_list = json.load(f) knio.output_objects[0] = v_variable_list import knime.scripting.io as knioimport pandas as pdmy_dictionary_imported = knio.input_objects[0]output_table = pd.DataFrame.from_dict(my_dictionary_imported, orient='columns')# These are the node's outputs that need to be populated:output_table = output_table.set_index(output_table.index.astype(str) + '_i5')# output_table = output_table.reset_index(drop=True)knio.output_tables[0] = knio.Table.from_pandas(output_table) locate and create/data/ folderwith absolute pathssee if the results matchwrite dictionarymy_dictionary.zipPath variable created forvar_model_pathmy_dictionary.zipread dictionarysee if the results matchwrite dictionaryread dictionarymy_dictionary.jsonmy_dictionary.jsonsee if the results matchread dictionaryCollect LocalMetadata Concatenate Python Script Model Writer Model Reader Python Script Concatenate Python Script Python Script Python Script Python Script Concatenate Python Script

Nodes

Extensions

Links