How can I write a variable (or a table with only one column and one row) to a textfile?
I have a table which I want to output as an HTML file to a webserver.
The table → HTML part is solved (e.g., Table to HTML String – KNIME Community Hub and/or doing it myself). I only need a way to write the string to a file.
I agree with you that using the CSV Writer for writing “plain text” files feels clumsy and a specific node would be an improvement. I’ve never really understood why there isn’t one.
The CSV Writer can generally do the job, but you’ll need to ensure that it is presented with only the one column, and probably the best answer I have for using it is to set the delimiter character to something that is unlikely to appear in your output.
For example the ¬ character is hardly used, or if you are using Windows, you can “dial in” numbers using the Alt key and the numeric key pad
Another option on Windows is to use the “emoji keyboard” popup. Press and hold the Windows Key, and then press the period/full-stop key “.”.
(Googling tells me this is Command + Control + Space on Mac, and possibly Ctrl ; on linux)
This will bring up the emoji window,
and you can choose a random emoji as the delimiter!
Beyond that, make sure double quotes are set to “never”
To my surpise, I found a solution. It’s called “Binary Objects”, which is not intuitive at all. A simple example would look like this:
The Table Creator creates a table with two columns: “test” containing the string I want to write to a file and “filename” containing the desired file.
The Strings to Binary Objects node even allows to set the encoding:
The Binary Objects to File node does support File System connectors.
The way to specify the filename is a little strange (one can not use variables), but works fine:
Two tables are converted to HTML, then the Filename is added as Column, then concatenate both, to Binary Objects and write the files via SSH. Works great.
Hi @masgo, I’m glad you got that working. I have recently used Binary Objects to Files myself, but only for binary objects! I hadn’t thought about using it for writing text.
The way it works for specifying the file (using the name of the file found in the column) is similar to the mechanism for nodes such as JSON Writer, as it is designed for being able to rapidly write each row to a separate file. In your case of course you have just the one row.
Having just looked at it again, along with your solution, I see why I’d not considered its use for writing plain text - it requires data to be in a binary-compatible format, which plain text Strings wouldn’t be and so of course Strings to Binary Objects resolves that issue.
Anyway, glad it works for you and thanks for sharing the solution. That might be useful in something I’m working on right now! I’d give your solution two “likes” if I could
p.s. and I think you should mark your own answer as “Solution”