Write a txt file for each key of a column

Hi,

I have a table that looks something like this:


I need to split this table based on each key (Key column) and create a txt file for each key.
I read something on this forum and i know I have to use the node “Table Row To Variable Loop Start” but I can’t do the same thing :confused:
Any idea is highly appreciated. Thank you very much in advance!

you have duplicate values in the key column so you might want to try group loop based on your key and write to separate csv/excel files.
br

2 Likes

thanks, how i can use it ?

hi @mosmel

as @Daniel_Weikert mentioned, you can either ‘groupby’ the data or filter the duplicates and use a loop approach.
i’m attaching my loop workflow for you to review
KNIME_filenames.knwf (95.7 KB)
image

happy knimining.
rgds

3 Likes

I appreciate your response. However, the duplicate lines are not visible in the output file. In the example you provided, not all the lines from “b2011” are included.

actually it is not possible to have the same filename in a directory.
in those workflow examples, the goal is to remove duplicate filenames , using a loop concept and writing the files to the selected directory.
however, if all the files or keys are unique, and we still want to create them, then we should give them unique file names.

in this example, the “rowid” is appended to the filenames to make them unique.
several node adjustments to be made.
image

rgds

I’m afraid I might not have expressed myself clearly earlier. My goal isn’t to have two files with the same key, but rather to have the lines associated with the same key in a SINGLE FILE.

sorry for my misunderstanding.
to do that, you need to set the mode of the csv writer node to “append”.
workflow needs to be adjusted like this.
image

rgds

1 Like

Thank you for your response, I will give that a try.

1 Like

The method you described works correctly for a single execution. However, the issue that arises is that during each workflow execution, the data is written to the same files, which is not desirable.

Another question, please: Is it possible to write to a folder that doesn’t exist yet and allow me to choose the writing path? Thank you in advance.

we can try getting advice from experts.
@takbb, would be grateful for your professional advice on this issue. :blush:

rgds

1 Like

Thank you @marzukim, :wink:

Hi @mosmel , to try to answer your additional questions, yes KNIME can create the path if it doesn’t exist. If you use the CSV Writer node (not the deprecated version) you will find it has an option to “create missing folders”. I’d recommend issuing this rather than the deprecated nodes.

What I would typically do is construct a String flow variable with the full path and filename that I require and then use String to Path (Variable) to make a Path variable that can be used with CSV Writer. You can alternatively use the Create File/Folder variables node.

If you don’t want the file names to be the same on each invocation of the workflow, then you need to decide whether you want to make part of the folder path or the filename change each time. You could include a string based on execution current date and time perhaps? Or did you have something else in mind?

You might find the following link useful as an example of incorporating execution date into output file names…

2 Likes

Thank you both very much for your responses. I have successfully managed to construct my workflow thanks to your assistance.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.