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:defconvertToInteger(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)
Did this help you find an answer to your question?
This post is closed to further activity.
It may be a question with a best answer, an implemented idea, or just a post needing no comment.
If you have a follow-up or related question, 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:defconvertToInteger(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)
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.