1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

data usage

Posted by spencer on Dec 16, 2008

Hello,

I just started using NodeBox and love the possibilities of code and design. I would like a little help with importing and using string data.

here is the start of my code:

colormode(CMYK)
size(400, 800)
 
    
import csv
reader = csv.reader(open("table.csv", "rb"))
for row in reader:
    print row[1],row[3]
it prints two columns of #'s

So my question is how do I utilize the individual numbers and or column of numbers.

Thanks


 
Posted by Josh Caswell on Dec 16, 2008

This is really a Python problem, and it's difficult to say here without seeing the data file you're working from. Is it possible for you to open your .csv file in a text editor and paste the first few lines here so we can look at it?

To give you a couple of thoughts right now, and if you'll forgive me for making assumptions about your skill, have you tried printing other members of row, or even the whole thing, to see what it looks like? You may simply be using the wrong indices (row[0] is the first member of the list)? Also, I've just done a couple of quick tests with csv.reader and a .csv file of my own, and I don't believe that you need to call open() with the "rb" option -- which means read in binary mode. I can't say that is definitely causing trouble, but you might try opening the file for normal read (which is the default):

open("table.csv")



Posted by spencer on Dec 16, 2008

Josh,

Thanks for you help. I am truly a novice, so this is probably very simple and I don't think I explained myself well before.

I want to create sized shapes based off of my .csv data.
How would I create a rect. based off of my data? and how do I specify NodeBox to grab this particular data?

My example is this I have four columns of data, each column has 100 rows of various numbers. I want to draw 100 rectangles based off of that data.

fill(0.2)
rect(Column#1, Column#2, Column#3, Column#4)
hope this makes more sense.





Good to know about the "rb"



Posted by Josh Caswell on Dec 17, 2008

What you are trying to do makes sense, and is straightforward. It's difficult, like I said, to know exactly what you need to do without seeing a few example lines from your data file, but let me give you a made-up example and then you should be able to go back to your file and figure it out.

I'm going to assume that the first few lines in your file look something like this:

25, 35, 45, 55
10, 15, 60, 65
75, 30, 95, 40
The data that you get each time through the for loop looks like this:
['25', ' 35', ' 45', ' 55']
This is a "list" made up of four "strings". There are NodeBox tutorials about these types of data which you should check out. To get each string, you access the list, which you have named "row":
print row[0]
print row[1]
print row[2]
resulting in this output
'25'
' 35'
' 45'
It's not important what the strings actually are. The important thing, since you are using the "comma-separated-values" (csv) module, is that the commas are separating the items that you want, and even more, that there isn't anything else on each line. If there are other items on the line, like this:
25, #, 35, #, 45, #, 55
then you're going to have to figure out what position they are in and access the list around them. This is why I suggest printing out the entirety of row to see what it looks like. If that was a line from your file, the data in row would look like this:
['25', ' #', ' 35', ' #', ' 45', ' #', ' 55']
and when you run those three print statements above you'll get:
'25'
' #'
' 35'
This is really the only problem you face: where are the items that you want located in the list called "row"? You've gotten most of the way there. Once you figure the locations out, you'll need to convert them so that NodeBox will use them as numbers. You can do this with the float() function:
rect( float(row[0]), float(row[1]), float(row[2]), float(row[3]) )
I'm not sure I can do much more for you, but one last note: The Python tutorial might also help you, despite its being kind of a heavy read: a file object, which you get when you call open():
f = open("table.csv")
can give you each of its lines without your having to use csv.reader, and a string can separate itself using a delimiter that you pick (like ", #, "). Using these more basic tools may make your problem easier to solve.

Hope you can figure it out!



Posted by spencer on Dec 18, 2008

Thanks josh,

This gives me much better direction than I had before. I think with the information you provided me I have a much better grasp of the concept.

After a few tries I may have a few more questions for you, but this is a great start.

Thanks again.
S



Posted by Josh Caswell on Dec 24, 2008

Sure thing, Spencer. Glad to be of help. If you have any further specific questions, I'll see what I can do.