为什么我的代码只在while循环中的一行循环而不是整个块?

时间:2016-07-26 20:49:44

标签: python parsing soap dataframe

对于不成熟的问题标题感到抱歉,但我绝望地需要帮助:

我的工作目标是创建一个脚本,从realtarget salesforce营销云API中提取所有记录。我已成功设置API调用,并成功将数据导入DataFrames。

我遇到的问题是双重的,我需要继续拉记录,直到" Results_Message"在我的代码中停止阅读" MoreDataAvailable"我需要设置逻辑,允许我从API调用或解析DataFrame中控制日期。

我的代码卡在第44行,其中"打印Results_Message"正在循环字符串" MoreDataAvailable"

到目前为止,这是我的代码,在第94和95行,您将看到我尝试直接从数据框解析日期,但没有运气,没有运气在第32行,我指定了日期:

import ET_Client
import pandas as pd


AggreateDF = pd.DataFrame()
Data_Aggregator = pd.DataFrame()
#Start_Date = "2016-02-20" 
#End_Date = "2016-02-25"
#retrieveDate = '2016-07-25T13:00:00.000'
Export_Dir = 'C:/temp/'


try:

    debug = False
    stubObj = ET_Client.ET_Client(False, debug)






    print '>>>BounceEvents'
    getBounceEvent = ET_Client.ET_BounceEvent()
    getBounceEvent.auth_stub = stubObj  
    getBounceEvent.search_filter = {'Property' : 'EventDate','SimpleOperator' : 'greaterThan','Value' : '2016-02-22T13:00:00.000'}
    getResponse1 = getBounceEvent.get()
    ResponseResultsBounces = getResponse1.results
    Results_Message = getResponse1.message
    print(Results_Message)
    #EventDate = "2016-05-09"
    print "This is orginial " + str(Results_Message)
    #print ResponseResultsBounces

    i = 1
    while (Results_Message == 'MoreDataAvailable'):
        #if i > 5: break
        print Results_Message
        results1 = getResponse1.results
        #print(results1)
        i = i + 1
        ClientIDBounces = []
        partner_keys1 = []
        created_dates1 = []
        modified_date1 = []
        ID1 = []
        ObjectID1 = []
        SendID1 = []
        SubscriberKey1 = []
        EventDate1 = []
        EventType1 = []
        TriggeredSendDefinitionObjectID1 = []
        BatchID1 = []
        SMTPCode = []
        BounceCategory = []
        SMTPReason = []
        BounceType = []

        for BounceEvent in ResponseResultsBounces:
            ClientIDBounces.append(str(BounceEvent['Client']['ID']))
            partner_keys1.append(BounceEvent['PartnerKey'])
            created_dates1.append(BounceEvent['CreatedDate'])
            modified_date1.append(BounceEvent['ModifiedDate'])
            ID1.append(BounceEvent['ID'])
            ObjectID1.append(BounceEvent['ObjectID'])
            SendID1.append(BounceEvent['SendID'])
            SubscriberKey1.append(BounceEvent['SubscriberKey'])
            EventDate1.append(BounceEvent['EventDate'])
            EventType1.append(BounceEvent['EventType'])
            TriggeredSendDefinitionObjectID1.append(BounceEvent['TriggeredSendDefinitionObjectID'])
            BatchID1.append(BounceEvent['BatchID'])
            SMTPCode.append(BounceEvent['SMTPCode'])
            BounceCategory.append(BounceEvent['BounceCategory'])
            SMTPReason.append(BounceEvent['SMTPReason'])
            BounceType.append(BounceEvent['BounceType'])

        df1 = pd.DataFrame({'ClientID': ClientIDBounces, 'PartnerKey': partner_keys1,
                       'CreatedDate' : created_dates1, 'ModifiedDate': modified_date1, 
                       'ID':ID1, 'ObjectID': ObjectID1,'SendID':SendID1,'SubscriberKey':SubscriberKey1,
                       'EventDate':EventDate1,'EventType':EventType1,'TriggeredSendDefinitionObjectID':TriggeredSendDefinitionObjectID1,
                       'BatchID':BatchID1,'SMTPCode':SMTPCode,'BounceCategory':BounceCategory,'SMTPReason':SMTPReason,'BounceType':BounceType})          
        #print df1
        #df1 = df1[(df1.EventDate > "2016-02-20") & (df1.EventDate < "2016-02-25")]
        #AggreateDF = AggreateDF[(AggreateDF.EventDate > Start_Date) and (AggreateDF.EventDate < End_Date)]
    print(df1['ID'].max())            
    AggreateDF = AggreateDF.append(df1)   
    print(AggreateDF.shape)
    #df1 = df1[(df1.EventDate > "2016-02-20") and (df1.EventDate < "2016-03-25")]    
    #AggreateDF = AggreateDF[(AggreateDF.EventDate > Start_Date) and (AggreateDF.EventDate < End_Date)]
    print("Final Aggregate DF is: " + str(AggreateDF.shape))

    #EXPORT TO CSV
    AggreateDF.to_csv(Export_Dir +'DataTest1.csv')
        #with pd.option_context('display.max_rows',10000):
            #print (df_masked1.shape)
        #print df_masked1 



except Exception as e:
    print 'Caught exception: ' + str(e.message)
    print e

在我的代码解析数据之前,我得到的数据的原始格式是SOAP响应,这就是它的样子(下图)。 是否可以直接从SOAP响应中解析基于EventDate的记录?

}, (BounceEvent){
   Client = 
      (ClientID){
         ID = 1111111
      }
   PartnerKey = None
   CreatedDate = 2016-05-12 07:32:20.000937
   ModifiedDate = 2016-05-12 07:32:20.000937
   ID = 1111111
   ObjectID = "1111111"
   SendID = 1111111
   SubscriberKey = "aaa@aaaa.com"
   EventDate = 2016-05-12 07:32:20.000937
   EventType = "HardBounce"
   TriggeredSendDefinitionObjectID = "aa111aaa"
   BatchID = 1111111
   SMTPCode = "1111111"
   BounceCategory = "Hard bounce - User Unknown"
   SMTPReason = "aaaa"
   BounceType = "immediate"

希望这是有道理的,这是我绝望的求助。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您似乎没有在循环中更新Results_Message,因此它总是会获得第29行中的值:Results_Message = getResponse1.message。除非涉及的代码与你没有分享,否则就是。