Grasshopper

algorithmic modeling for Rhino

Hey everyone:

I am currently working on a project that imports a .csv with basic data points into grasshopper. I understand the problem well enough, but I am having technical difficulties trying to get grasshopper to read TABS as TABS or even as "\t". Right now it appears as if the parser is simply removing these characters altogether. This makes it difficult to read the data, as you can imagine.

Am I right in assuming that grasshopper removing the Tab characters? Or are they just not being displayed within the panel? If it is removing the Tabs, does someone have a solution to avoid this? Any help would be appreciated.

I have tried using Lunchbox's Excel Reader component as well as the generic Read File component that comes standard with GH. Thanks!

- William

Views: 2479

Replies to This Discussion

An easy way to read certain sheets might be to use the ghowl plugin. It will let you pick which sheet to use when you import it.

Hi William,

When you say "tabs" do you mean sheets? CSV files do not support sheets. A CSV creates one data set with each item separated by a comma. I'd try saving your file as an XLSX or some other extension that supports multiple sheets.

Hope this helps. Let me know if I misunderstood something.

Cheers,

/SPM

Stephen,

the "tab" is the separator (like , or ; or /). And indeed, separating by tab is bad practice...

One idea is to load the *.csv in excel or some text editor and rename the tabs in ";"

Sublime Text is pretty neat for these kind of operations.

Hey Anders: by "these kind of operations" do you mean find / replace? I've used SublimeText in the past but mostly for web-related applications, and my experience using it is only minimal. Ideally, the finding / replacing of the "tabs" would be handled inside of a ghPython component or even the ReadFile component of the GH definition by overloading its built-in parser method. But if a little "manual" labor (using the term quite loosely) was required to prep the *.CSV for the GH definition, it wouldn't be the end of the world.

Yes that's exactly what I meant. Sublime has a bunch of nifty features such a multiple cursors, searching using regular expressions etc. In the link above regex is used to find all tabs and then replaced. But I also use for things like removing line breaks in text pasted from a pdf etc. I think Python also supports regex, so you should be able to do something like:

myString.strip("\t")

Although that's just from memory, might want to google it.

Edit: Actually, it's probably more like this:

myString.replace("\t","")

Great! Thanks, Anders.

You're correct in assuming that Python supports regex. While the support is not native to the Python language, I believe that the stdlib provides the support. This obviates my need to rely on a Unicode --> ASCII conversion method to define a [TAB] within my replace() function.

Ah. A nomenclature thing then, huh? Thanks for the clarification, Raul. Will the [SplitText] component not work? I've used it often when bringing CSV files into Grasshopper. I've also used Python for this kind of thing. Still, a messy way to handle data.

Sublime Text seems to do the trick. Thanks for bringing that to our attention, Anders.

Here's a simple Python code I used to split lines separated by commas in a CSV. I used it in GHPython but should also work in the Rhino Python Script Editor.

#open file for reading
file = open(filename, 'r')


#read lines into variable called lines
lines = file.readlines()


#close file
file.close()

for line in lines:

#remove \n
line = line.strip("\n")
#split line with comma: creates individual lists of strings
csvInfo = line.split(',')

Thanks for this, Stephen. I think I am going to adapt this parser to do what SublimeText might do. I may replace the line.split(',') with a line.replace('[TAB]', ','), so that the file can be prepped to use GH's built-in SplitText component as you mentioned. Using the GH component versus building in that functionality is really more a matter of style; I would like to use as little custom components as possible. Obviously a problem may arise trying to get Python to recognize the '[TAB]' character. I may need to use a Unicode --> ASCII conversion method to handle this. I'll try implementing it today and report back the outcome.

Here's the code I ended up with, and it works just fine. Thanks for the help, everyone.

----------------------------------------------------------

# open file

f = open(filename, 'r')

# create list to store converted lines
converted = []

# read each line of f
for line in f.readlines():
    # convert each line and add it to converted
    converted.append(line.replace('\t',','))

# close file
f.close()

----------------------------------------------------------

I noticed when creating a blank list (i.e. "converted = []"), I could not construct one using the list() function. Is this something that's different between regular Python and IronPython? Which version of IronPython does the ghPython component support? I am trying to link the proper documentation to the text editor with which I am going to code. Does anyone have any suggestions?

You should be able to instantiate a list using list(), although it is not recommended. What sometimes happens is that one names a variable the name of a function (i.e. naming something "list"). Could that be the error?



You can check the version like so:

import sys
print sys.version




RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service