Hi. I have a table with an attribute with string values such as "39.1234". I'm trying to extract the integer of this to a new attribute using Python. So something like this
However, this code gives the following error and i've tried various other options without success. Any ideas how to to extract the integer from a string with Python? Thanks
Best answer by jeroenstiers
Hi ericarmitage,
As david_r already mentioned, it is important to use the round function when converting from a float towards an integer. Below I also implemented a try-except block to show a warning in the log if the conversion fails.
import fme import fmeobjects
logger = fmeobjects.FMELogFile()
# Template Function interface: def convertToInteger(feature):
string = feature.getAttribute('string')
try: integer = float(string) integer = round(integer) integer = int(integer) feature.setAttribute('integer', integer) except: logger.logMessageString('The following string couldn\'t be converted towards an integer: %s.' % (string), fmeobjects.FME_WARN)
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.
Notice the try/except-block that ignores any potential errors when casting vStartMiles into an integer. You could easily adapt the above to treat these special cases as you see fit.
Now, having said that, I'm not quite sure what you're trying to accomplish with the above. FME is normally pretty good at casting its attributes to integers when necessary, without user intervention.
Thanks David. I've filtered out the features with empty strings before the Python caller and it works ok. However, the int function doesn't seem to work on a decimal string but vStartMiles2 = int(float(vStartMiles)) does
Thanks David. I've filtered out the features with empty strings before the Python caller and it works ok. However, the int function doesn't seem to work on a decimal string but vStartMiles2 = int(float(vStartMiles)) does
Eric
That is correct, you cannot cast a string looking like a float as an integer.
Be aware, however, that int(2.9) = 2 and not 3! Use round(float(vStartMiles) to get the value 3.
As david_r already mentioned, it is important to use the round function when converting from a float towards an integer. Below I also implemented a try-except block to show a warning in the log if the conversion fails.
import fme import fmeobjects
logger = fmeobjects.FMELogFile()
# Template Function interface: def convertToInteger(feature):
string = feature.getAttribute('string')
try: integer = float(string) integer = round(integer) integer = int(integer) feature.setAttribute('integer', integer) except: logger.logMessageString('The following string couldn\'t be converted towards an integer: %s.' % (string), fmeobjects.FME_WARN)