Grasshopper

algorithmic modeling for Rhino

Hey guys,

I am new to regular expressions and I am experiencing some issues. Basically I am receiving an error from GH when I try to extract List Items from a list of strings (size = 2435). When trying to cast these strings into numbers, GH returns the error: "Data conversion failed from Text to Number."

So I am trying to debug this error and find the List Item that is causing the trouble. I would like to automate this, since the list size is rather large to handle manually. I figured that I could use ghPython and RegExs to find the non-convertible character(s) that are the source of the problem. After browsing StackOverflow and Python's documentation, I found that I could use the following code:

import re

pattern = re.compile('^[0-9]+$', re.IGNORECASE)

pattern.search(x)

x is my list of Strings. My hope is to shoot x through my ghPython component as a grafted list, receiving 2435 Boolean values, which I would then shoot through SiftPattern to find the problem characters. 

The trouble is that I am not receiving any Boolean values from my ghPython component; instead I am receiving <null>. Any feedback would be greatly appreciated. It may very well be that my mechanics of using GH and ghPython are incorrect. Also, if you have any other suggestions about how to approach this problem, I am amenable to those as well. Just to reiterate, I entirely new to RegExs, so it could be that my code is totally nonsensical. Either way, thank you for reading this. Please let me know if there is any additional information that I can provide that may help diagnose the problems here.

Thanks.

Views: 754

Replies to This Discussion

The plot thickens...

Here are two screenshots where I am simply trying to cast one individual item from the list as a number. It doesn't work in the first example yet it works perfectly well in the second example.

The first screenshot has an item that is coming from the List of strings that CANNOT be cast into numbers. The second screenshot has an item that comes from a List of strings with the same structure as the first example, except these numbers can all be cast into numbers without any errors...

Is it possible that there is an EOL or '\n' character lurking somewhere in the background? And if so...why don't Panels display these characters? Is there some better component available within GH that can display more information about text files? GH in the past hasn't displayed '\t' characters either. Perhaps this type of functionality is already available and I am unaware of it? Or maybe it is a setting within the already existing panel components (e.g. right-click --> display hidden character information)? Or...maybe it is something that should be added to GH 2. Just musing aloud. Either way, any comments are welcomed!

Simple, do a test on your strings using isnumeric method to see if numeric or not and only do a numeric operation on those that are:

http://www.tutorialspoint.com//python/string_isnumeric.htm

Thanks for the reply, Steve. Unfortunately this method won't return True even on strings that GH can successfully cast. The reason for this is that it goes char-by-char and checks if each is a number. That's all good and well until it reaches the decimal point of the number (1.000000), where it rightfully returns False. Here's a screenshot to illustrate this point.

I appreciate the reply, and I think I am getting closer to solving the issue. Now I just need to find a Python function similar to isnumeric() that can validate decimal numbers as convertible... It looks like Python's unicode.isdecimal() function may do the trick. I'm not sure though

Still curious about solving this via RegExs, so if there are any RegEx hounds out there, please don't hesitate to edify me.

It helps a lot if you share your code.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service