Skip to main content
Solved

String Value to integer in Python


Forum|alt.badge.img

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

import fme

 

import fmeobjects

 

def processFeature2(feature):

 

 

vStartMiles = feature.getAttribute('Start_Mileage')

 

 

vStartMiles2 = int(vStartMiles)

 

 

feature.setAttribute("Start_Miles", vStartMiles2)

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)

0684Q00000ArJfDQAV.png

0684Q00000ArJLiQAN.png

View original
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.

5 replies

Forum|alt.badge.img

this is the error

Python Exception <ValueError>: invalid literal for int() with base 10: ''

Error encountered while calling function `processFeature2'

PythonFactory failed to process feature

A fatal error has occurred. Check the logfile above for details

Bridge failed to output feature on tag `PYOUTPUT'

PythonFactory failed to process feature

 


david_r
Evangelist
  • January 19, 2016

Hi

Looks like Start_Mileage can sometimes be an empty string, you will have to handle that to avoid or ignore the ValueError exception. Something like:

vStartMiles = feature.getAttribute('Start_Mileage')
try:
    vStartMiles2 = int(vStartMiles)
    feature.setAttribute("Start_Miles", vStartMiles2)
except:
    pass

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.

David


Forum|alt.badge.img

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


david_r
Evangelist
  • January 19, 2016
ericarmitage wrote:

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.

David


Forum|alt.badge.img+7
  • Best Answer
  • January 20, 2016


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)

0684Q00000ArJfDQAV.png

0684Q00000ArJLiQAN.png


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings