Extract Column Headers from a CSV file?
I am visualizing CSV files. I can slice and dice the data but would also like to display and manipulate the column headers.
For some projects I need to do generic lookups without having to hand-code new key values every time I change a file. For example I'd like to simply display the contents of any CSV file - including the headers - as a full-fledged NodeBox visualization (with fonts, colors, grid spacing etc. under my full control) by simply changing the file in the Import Csv node.
All I need is some way to return the column header keys as individual strings. Even just a way of counting the number of columns in a CSV file would be helpful.
How do I do this in Nobebox 3?
Thanks in advance for any suggestions or comments!
John
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
Support Staff 1 Posted by lucasnijs on 10 Feb, 2015 07:41 PM
In NB3 there is no direct way of getting this. I agree it should be possible. The only way at the moment is cheating by adding a row in your csv file: repeating the headers. Later on you have to omit these rows. I agree this is not a real solution. We'll look into this and see if this can not be handled in the next release.
regards,
Lucas
Support Staff 2 Posted by john on 10 Feb, 2015 11:46 PM
Thanks for the response, Lucas.
If you are compiling a wish list I have another related feature request.
It would be very handy to create a two dimensional zip map that would let me create and pass internal CSV data structures from node to node. As far as I can tell, the current zip map can only create a vector of label-value pairs, not a matrix with multiple index rows.
The output of this improved zip map node would be a data structure just like the output of import CSV. Other nodes could then filter this output and do look ups on it. This would be a very powerful and useful capability.
Doesn't hurt to ask.
Thanks again,
John
Support Staff 3 Posted by lucasnijs on 11 Feb, 2015 03:40 PM
Hello John,
don't forget you can add your own code libraries. Enclosed is a file with a custom node that gets the headers as you want. Then you can count them too if you want...
Have fun,
Lucas
PS: the csvheaders.py needs to be at the same root.
Support Staff 4 Posted by john on 12 Feb, 2015 02:06 AM
Nice! Thanks, Lucas.
I was aware of the ability to add my own code libraries, but have been trying to resist it while I'm learning how to think in NodeBox. I find that staying within the standard nodes forces me to explore them more deeply. At first it often looks like something can't be done, but when you look a little deeper there is usually a way.
But it's impressive how simple it was to add that custom node. Only six short lines of code. I'll keep this in mind next time I get stuck.
Thanks again!
John
Support Staff 5 Posted by Stefan Gabriels on 12 Feb, 2015 06:43 AM
In the next release there will be a getCSVHeaders node.
Support Staff 6 Posted by Frederik De Ble... on 17 Feb, 2015 11:19 AM
Hi,
The new release, 3.0.43, contains the functionality you need. However, the node is called "keys", since it can retrieve the keys of any kind of map-related data, including CSV nodes.
Another useful feature is that you can append it after a graphic object, in which case it will list the properties of that object that you can retrieve using the lookup node.
https://www.nodebox.net/download/
Best,
F
john closed this discussion on 25 Feb, 2015 07:56 AM.