How to Convert Strings to Numbers

Converters enable you to specify the way data should be converted between the external source and a CI's attributes.

A CSV file contains records of type string. However, some of the record values need to be handled as numbers. This is done by adding a converter element to the map element (in [your mapping file name].xml):

        <converter module="import_converters"></converter>

The import_converters.py file (Adapter Management > Resources pane > Packages> External_source_import > Scripts) contains a set of the most commonly needed converters and types:

  • toString

  • stringToInt

  • stringToLong    

  • stringToFloat

  • stringToBoolean

  • stringToDate

  • stringToDouble

  • skipSpaces

  • binaryIntToBoolean

  • stringToBytesArray

  • stringToZippedBytesArray

  • Example of a Converter

    A CSV file contains the following row:

    Usain, 21, Male
    

    This row must be mapped to the Person CIT that includes name (Usain), age (21), and gender (Male) attributes. The age attribute should be of type integer. Therefore, the string in the CSV file must be converted to an integer in the CIT to make it compliant with the CIT attribute type, before the Person CIs can retrieve the age values.

    This is done by adding a converter element to the map element:

        <map>
            <attribute>age</attribute>
            <column>2</column>
            <converter module="import_converters">stringToInt</converter>
        </map>
    

    module="import_converters". This attribute specifies from which module the converter is to be retrieved. A module is a Jython script file that contains a set of converter methods, in this case, import_converters.py.

    stringToInt. The name of the converter. In the import_converters.py file, the method is written as follows:

    def stringToInt(value):
        if value is not None:
            return int(value.strip())
        else:
            return 0
    

Custom Converters

You can write your own custom converters: Add a new method to the import_converters.py file or create your own script and add a set of converter methods to it. Call the method with the name of the script, for example:

<converter module="your_converter_script">[your_converter_method]
</converter>