简体   繁体   English

使用Beatbox从greenplum获取数据并将其加载到Salesforce。 AttributeError:“小数”对象没有属性“替换”

[英]Using Beatbox to fetch data from greenplum and load in to salesforce. AttributeError: 'Decimal' object has no attribute 'replace'

I'm Using Beatbox to fetch data from greenplum db and load it in to salesforce. 我正在使用Beatbox从greenplum db中获取数据并将其加载到Salesforce。

The fetched data looks like this: 提取的数据如下所示:

    {'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}

And this gives the following error: 这会产生以下错误:

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

if I remove the items with value Decimal('xxx.xx') and None, it's working fine. 如果我删除值为Decimal('xxx.xx')和None的项目,则工作正常。

How should I handle Decimal and None values? 我应该如何处理小数和无值?

Complete Code: 完整的代码:

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])

Complete Output(with error): 完整输出(有错误):

        {'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'

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Beatbox python API从Salesforce中提取原始数据 - How to extract raw data from Salesforce using Beatbox python API AttributeError:&#39;ModuleSpec&#39;对象没有属性&#39;load_data_wrapper&#39; - AttributeError: 'ModuleSpec' object has no attribute 'load_data_wrapper' AttributeError:类型对象&#39;WebTablePage&#39;在使用pytest的selenium中没有属性&#39;load&#39; - AttributeError: type object 'WebTablePage' has no attribute 'load' in selenium using pytest AttributeError:“ Morsel”对象没有属性“ replace” - AttributeError: 'Morsel' object has no attribute 'replace' AttributeError:&#39;SelectorList&#39;对象没有属性&#39;replace&#39; - AttributeError: 'SelectorList' object has no attribute 'replace' AttributeError:“ NoneType”对象没有属性“ replace_with” - AttributeError: 'NoneType' object has no attribute 'replace_with' AttributeError:&#39;function&#39;对象没有属性&#39;replace&#39; - AttributeError: 'function' object has no attribute 'replace' AttributeError:“列表”对象没有属性“替换”, - AttributeError: 'list' object has no attribute 'replace', Kivy ~ AttributeError: 'TextInput' object 没有属性 'replace' - Kivy ~ AttributeError: 'TextInput' object has no attribute 'replace' AttributeError:“索引”object 没有属性“替换” - AttributeError: 'Index' object has no attribute 'replace'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM