[Python] I don't understand double loop.

Asked 1 years ago, Updated 1 years ago, 114 views

This is the code related to the securities company API written by someone else. After passing the value to BlockRequest() in the middle, it receives data from the securities company server through the cpSvr7254.GetHeaderValue(1) method and returns or outputs the data to GetDataValue.

But if you look in the middle, there's a part with for inside. I'm not sure how you read this. After completing all of them, the data values are as in the Excel file below. Since there are 14 horizontal data, I understand that 14 was entered in 'forixColinrange(14)' But I'm not sure how this reads the data. Do I read it vertically, enter it in tempdata, and then enter it horizontally? I tried to solve it by myself, but it's too hard. I look forward to hearing from the master.

import time
import win32com.client
def subCpSvr7254(m_code, m_FromDate, m_ToDate):
   ## Set API instead
    cpSvr7254 = win32com.client.Dispatch("CpSysDib.CpSvr7254")
    cpSvr7254.SetInputValue(0, m_code) #StockCode
    cpSvr7254.SetInputValue(1, '6') #DurationSelect 0:DurationSelect, 1:1 month, ..., 4:6 month
    cpSvr7254.SetInputValue(2,m_FromDate) #StartDate
    cpSvr7254.SetInputValue(3, m_ToDate) #EndDate
    cpSvr7254.SetInputValue(4, '0') #0:Net #1:weight
    cpSvr7254.SetInputValue(5, '0') #Investor
    **cpSvr7254.BlockRequest()**

    **numData=cpSvr7254.GetHeaderValue(1)
    #print(numData)
    data=[]
    for ixRow in range(numData):
        tempData=[]
        for ixCol in range(14): 
            tempData.append(cpSvr7254.GetDataValue(ixCol, ixRow))
        data.append(tempData)**

    #Continuous performance
    while cpSvr7254.Continue:
        cpSvr7254.BlockRequest()
        numData = cpSvr7254.GetHeaderValue(1)
        #print(numData)
        for ixRow in range(numData):
            tempData=[]
            for ixCol in range(14):
                tempData.append(cpSvr7254.GetDataValue(ixCol, ixRow))
            data.append(tempData)
            time.sleep(0.1)

    return data

from subDS import subCpSvr7254
from pandas import DataFrame

if __name__ == "__main__":

    Code='A005930' #Samsung Electronics Code
    FromDate = "20161201" # Request Start Date
    toDate = "20161223" # Last Date of Request

    ### Importing data
    data=subCpSvr7254(code, fromDate, toDate)
    print(data)
    df=DataFrame (data, columns=['date', 'individual', 'foreign', 'institutional', 'financial investment', 'insurance', 'investment', 'bank', 'other finance', 'finance', 'other corporations', 'other foreigners', 'private equity'])
    df.to_csv('subCpSvr7254.csv')

python nested-loops

2022-09-22 13:13

1 Answers

data=[]
    for ixRow in range(numData): # first loop
        tempData=[]
        for ixCol in range(14):  #second loop
            tempData.append(cpSvr7254.GetDataValue(ixCol, ixRow))
        data.append(tempData)**

If you take a look at this part, First loop is looping rows as many as the number of data. One row in an Excel, corresponding to one data record.

You enter the second loop with an empty list called tempData defined for one data, which loops the column. That's the column of the Excel file. The value of the column corresponding to the current data in the API (date, person,...) etc.) By taking and appending it to tempData, tempData becomes one data (one row in Excel).

After storing all the information about one data, the second loop ends and the completed one data is appended to the final data list. And if you move on to the next data, reset the tempData back to the empty list.

And then you go around the second loop again and complete another data.

When you look at the double loop, it's a little more comfortable if you first check how the innermost loop rotates and think about how it rotates with the outer loop.


2022-09-22 13:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.