Download User Guide: Map Translator Control

Transcript
Python Scripts
string.strip(name1);
•
Create a string variable name1 by taking the first 6 characters of name2
name1 = name2[0:5];
•
Create a string variable name1 by removing the first 2 characters from name2.
name1 = name2[2:];
•
Create a string variable name1 by retaining the first 3 characters from name2.
name1 = name2[:2];
•
Another way to pad a variable is with string.zfill. The following creates a of value '00000012345'.
string.zfill('12345', 11);
•
Zfill understands periods and negative signs as part of the padding. zfill will not truncate numbers
that are too long. The following will give a value of is '-0000003.14'.
string.zfill('-3.14', 11);
Error Handling: Try—Except
If errors happen during data manipulation with python, it can crash the translator. To avoid this, you
should enclose statements within a try—except routine, as in the following example that creates a number padded to 16 characters.
import string;
account = original.getString('Customer Number');
paddedaccount=''
try:
paddedaccount = string.zfill(account,16);
original.setString('CustomerMergeNumber',paddedaccount);
except:
log.warning('Error Padding Account');
In the above example, a Customer Number such as 2354256 would create a field in the Map Translator
Data Types called CustomerMergeNumber, and for this consumer, would have the value of
0000000002354256. If there is a problem creating the CustomerMergeNumber, the Map Translator will
continue to translate.
In order to catch the Java line number of the error, you will need a more elaborate phrase, as follows.
from com.PartnerSoft.util.SystemLog
try:
[do something]
except Exception, something:
SystemLog.singleton().error(something)
71