使用Beatbox从greenplum获取数据并加载到salesforce。 AttributeError:'Decimal'对象没有属性'replace'

时间:2012-12-08 20:36:59

标签: python api salesforce

我正在使用Beatbox从greenplum db获取数据并将其加载到salesforce。

获取的数据如下所示:

    {'loan_id__c': '25104776', **'interest_balance__c': Decimal('188.44'),**
 'number_of_payments_made__c': 29.0, 'days_past_due__c': 53.0, 'type': 'OPPORTUNITY',
 'loan_status__c': None, 'last_payment__c': datetime.date(2012, 5, 3),
 'collection_type__c': 'ACH', 'dollars_past_due__c': Decimal('267.43'),
 'total_balance__c': Decimal('297.03'), 'principal_balance__c': Decimal('278.59'), 
'error_payment_count__c': 4.0, 'credit_sponsor_funding_date__c': None,
 'fee_balance__c': Decimal('190.00'), 
'last_updated_date_time__c': datetime.datetime(2012, 9, 17, 0, 0), 'number_of_missed_payments__c': 20.0}

这会产生以下错误:

File "C:\Python27\lib\xml\sax\saxutils.py", line 39, in escape
data = data.replace("&", "&")
AttributeError: 'Decimal' object has no attribute 'replace'

如果我删除值为Decimal('xxx.xx')和None的项目,则表示工作正常。

我应该如何处理十进制和无值?

完整代码:

import sys
import beatbox
import xmltramp
import datetime
import xlrd
import csv
import psycopg2
from psycopg2.extras import RealDictCursor

sf = beatbox._tPartnerNS
svc = beatbox.Client()
beatbox.gzipRequest=False

loginResult = svc.login("xxx@xxx.com", "xxxxx")
print "sid = " + str(loginResult[sf.sessionId])
print "welcome " + str(loginResult[sf.userInfo][sf.userFullName])

connection_string = "dbname=gp_user user=gp_an password=gpm host=10.xxx.x.xx port=xxxx"

#create a connection to Greenplum
gp_connection = psycopg2.connect(connection_string)


query = "SELECT 'OPPORTUNITY' as type, loan_id__c,interest_balance__c,dollars_past_due__c,number_of_missed_payments__c,last_updated_date_time__c,loan_status__c,principal_balance__c,error_payment_count__c,days_past_due__c,last_payment__c,fee_balance__c,collection_type__c,number_of_payments_made__c,total_balance__c,credit_sponsor_funding_date__c FROM sf_opportunity where loan_id__c IN ('25104776','2395362','245715')"

#create a cursor. the cursor will be what executes your queries
gp_cursor = gp_connection.cursor(cursor_factory=RealDictCursor)
gp_cursor.itersize = 100
gp_cursor.execute(str(query))

for row in gp_cursor:
    #print row
    sobjects = []
    sobjects.append(row)
    print sobjects
    ur = svc.upsert('loan_id__c', sobjects)
    if str(ur[sf.success]) == 'true':
        print "id " + str(ur[sf.id])
    else:
        print "error " + str(ur[sf.errors][sf.statusCode]) + ":" + str(ur[sf.errors][sf.message])

完成输出(有错误):

        {'loan_id__c': '25104776', 'interest_balance__c': Decimal('188.44'),
 'number_of_payments_made__c': 29.0, 'days_past_due__c': 53.0,
 'type': 'OPPORTUNITY', 'loan_status__c': None, 
'last_payment__c': datetime.date(2012, 5, 3), 'collection_type__c': 'ACH',
 'dollars_past_due__c': Decimal('267.43'), 'total_balance__c': Decimal('297.03'),
'principal_balance__c': Decimal('278.59'), 'error_payment_count__c': 4.0,
 'credit_sponsor_funding_date__c': None, 'fee_balance__c': Decimal('190.00'),
 'last_updated_date_time__c': datetime.datetime(2012, 9, 17, 0, 0),
 'number_of_missed_payments__c': 20.0}

    Traceback (most recent call last):
      File "gp2.py", line 39, in <module>
        ur = svc.upsert('loan_id__c', sobjects)
      File "C:\Python27\beatbox.py", line 99, in upsert
        return UpsertRequest(self.__serverUrl, self.sessionId, externalIdName, sObje
    cts).post(self.__conn)
      File "C:\Python27\beatbox.py", line 294, in post
        rawRequest = self.makeEnvelope();
      File "C:\Python27\beatbox.py", line 269, in makeEnvelope
        self.writeBody(s)
      File "C:\Python27\beatbox.py", line 420, in writeBody
        self.writeSObjects(s, self.__sObjects)
      File "C:\Python27\beatbox.py", line 344, in writeSObjects
        self.writeSObjects(s, o, elemName)
      File "C:\Python27\beatbox.py", line 351, in writeSObjects
        s.writeStringElement(_sobjectNs, fn, sObjects[fn])
      File "C:\Python27\beatbox.py", line 190, in writeStringElement
        self.characters(value)
      File "C:\Python27\beatbox.py", line 210, in characters
        self.xg.characters(s)
      File "C:\Python27\lib\xml\sax\saxutils.py", line 162, in characters
        self._write(escape(content))
      File "C:\Python27\lib\xml\sax\saxutils.py", line 39, in escape
        data = data.replace("&", "&amp;")
    AttributeError: 'Decimal' object has no attribute 'replace'

1 个答案:

答案 0 :(得分:0)

看起来saxutils.py想要字符串格式的数据,所以最好的选择是将data中的所有值转换为字符串类型。

data_as_string = {k: str(v) for k,v in data.items()}