Extract Column Headers from a CSV file?

john's Avatar

john

08 Feb, 2015 12:36 AM

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

  1. Support Staff 1 Posted by lucasnijs on 10 Feb, 2015 07:41 PM

    lucasnijs's Avatar

    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

  2. Support Staff 2 Posted by john on 10 Feb, 2015 11:46 PM

    john's Avatar

    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

  3. Support Staff 3 Posted by lucasnijs on 11 Feb, 2015 03:40 PM

    lucasnijs's Avatar

    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.

  4. Support Staff 4 Posted by john on 12 Feb, 2015 02:06 AM

    john's Avatar

    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

  5. Support Staff 5 Posted by Stefan Gabriels on 12 Feb, 2015 06:43 AM

    Stefan Gabriels's Avatar

    In the next release there will be a getCSVHeaders node.

  6. Support Staff 6 Posted by Frederik De Ble... on 17 Feb, 2015 11:19 AM

    Frederik De Bleser's Avatar

    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

  7. john closed this discussion on 25 Feb, 2015 07:56 AM.

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

Recent Discussions

22 Feb, 2024 05:51 AM
22 Feb, 2024 01:11 AM
21 Feb, 2024 05:27 PM
21 Feb, 2024 04:49 PM
16 Feb, 2024 05:24 PM

 

06 Feb, 2024 04:45 AM
19 Jan, 2024 09:50 AM
13 Jan, 2024 03:47 AM
05 Jan, 2024 05:10 PM
05 Jan, 2024 04:54 PM
04 Jan, 2024 11:49 PM