将父变量调用到列表中

Calling Parent Variables into List

如何确保 parseTwoPoleBreakres 中的变量 brk1_int_c,brk1_ext_c,brk2_int_c,brk2_ext_c 被放入 inputList 而不是在函数外部调用 brk1_int_c,brk1_ext_c,brk2_int_c,brk2_ext_c

我的 parseTwoPoleBreakers 功能有问题。我正在尝试将列表 inputList 输入到函数中,因此我知道将 brk1_int_c、brk1_ext_c、brk2_int_c、brk2_ext_c 放置在该列表中的什么位置。我输入的列表很好,但是变量的 brk1_int_c,brk1_ext_c,brk2_int_c,brk2_ext_c 没有在我的 parseTwoPoleBreakers 函数中的 inputList 中被调用。

我要调用的变量是:

brk1_int_c=str(df1['Unnamed: 1'][aRowNum])#starts at row 7,13,19,25,31,37,43,49. addition of 6
brk1_ext_c=str(df1['Unnamed: 2'][aRowNum])
brk2_int_c=str(df1['Unnamed: 1'][bRowNum])
brk2_ext_c=str(df1['Unnamed: 2'][bRowNum])`

位于 'parseTwoPoleBreakers' 函数内,而是

brk1_int_c = '' brk1_ext_c = '' brk2_int_c = '' brk2_ext_c = ''

被调用,结果我得到以下

的输出

print two_pbwmco_a_bString print "breaker id" + str(breakerid)

输出结果:

NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id1
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id2
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id3
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id4
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id5
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id6
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id7
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id8
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id9
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id10
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id11
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id12
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id13
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id14
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id15
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id16
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id17
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id18
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id19
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id20
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,,,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1

breaker id21

我的代码如下:

import pandas as pd
with pd.ExcelFile("C:/Users/original.xlsx",
                  sheetname=['pnl1 Data ','pnl2 Data','pnl3 Data','pnl4 Data']) as xlsx:
    df1 = pd.read_excel(xlsx, 'pnl1 Data ', skiprows=9, parse_cols="B:H", keep_default_na='FALSE', na_values=['NULL'])
    df2 = pd.read_excel(xlsx, 'pnl2 Data', skiprows=9, parse_cols="B:H", keep_default_na='FALSE', na_values=['NULL'])
    df3 = pd.read_excel(xlsx, 'pnl3 Data', skiprows=9, parse_cols="B:H", keep_default_na='FALSE', na_values=['NULL'])
    df4 = pd.read_excel(xlsx, 'pnl4 Data', skiprows=9, parse_cols="B:H", keep_default_na='FALSE', na_values=['NULL'])

# 2. convert data into a single row, in the correct order, and into csv format
df1numOfRows = len(df1.index)

# 2a. you need to look at the breaker table to see what the highest breaker id is and then use the number one higher than that
#fi_breakerTable = open(document,"r")  

# 2b. store variables in a list that will be written to csv
breakerid=0
deviceid=0
NULL="NULL"

# 2bc. FDC Feed Braeker "List"
g_int_c=str(df1['Unnamed: 1'][6])
g_ext_c=str(df1['Unnamed: 2'][6])
breakertype="NULL"#should I keep this null or does it equal "FDC Feed Breaker"?
n_int_c=str(df1['Unnamed: 1'][5])
n_ext_c=str(df1['Unnamed: 2'][5])
a_int_c=str(df1['Unnamed: 1'][2])
a_ext_c=str(df1['Unnamed: 2'][2])
b_int_c=str(df1['Unnamed: 1'][3])
b_ext_c=str(df1['Unnamed: 2'][3])
c_int_c=str(df1['Unnamed: 1'][4])
c_ext_c=str(df1['Unnamed: 2'][4])
a_b=str(df1['Unnamed: 2'][0])
b_c=str(df1['Unnamed: 6'][0])
c_a=str(df1['Pnl 1 Data'][0])
fdcFeedBreakerList = [g_int_c,g_ext_c,NULL,NULL,n_int_c,n_ext_c,NULL,NULL,a_int_c,a_ext_c,NULL,NULL,b_int_c,b_ext_c,NULL,NULL,c_int_c,c_ext_c,NULL,NULL,a_b,c_a,b_c,NULL,breakertype,deviceid,breakerid] #3 pole breake riwht monitoring, an nuetral and a group (current & voltage)
breakerid+=1
fdcFeedBreakerString=",".join(map(str,fdcFeedBreakerList))
fdcFeedBreakerString=fdcFeedBreakerString+"\n"
print "fdcFeedBreakerString" + str(fdcFeedBreakerString)


# 2bc. FDC Feed Braeker "List"
brk1_int_c = ''
brk1_ext_c = ''
brk2_int_c = ''
brk2_ext_c = ''
def parseTwoPoleBreakers(breakerStartVal1,breakerStartVal2,inputList,i):
    #global brk1_int_c,brk1_ext_c,brk2_int_c,brk2_ext_c
    additionValue=(i*6)
    #2 Pole breaker with monitring (current only) (a-b)
    aStartVal=breakerStartVal1
    bStartVal=breakerStartVal2
    if i>=1:
        aRowNum=aStartVal
        bRowNum=bStartVal
    else:
        aRowNum=aStartVal+additionValue
        bRowNum=bStartVal+additionValue   
    brk1_int_c=str(df1['Unnamed: 1'][aRowNum])#starts at row 7,13,19,25,31,37,43,49. addition of 6
    brk1_ext_c=str(df1['Unnamed: 2'][aRowNum])
    brk2_int_c=str(df1['Unnamed: 1'][bRowNum])
    brk2_ext_c=str(df1['Unnamed: 2'][bRowNum])
    two_pbwmco_a_b=inputList
    two_pbwmco_a_bString=",".join(map(str,two_pbwmco_a_b))
    two_pbwmco_a_bString=two_pbwmco_a_bString+"\n"
    print two_pbwmco_a_bString
    print "breaker id" + str(breakerid)

two_pbwmco_a_bList2=[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,brk1_int_c,brk1_ext_c,NULL,NULL,brk2_int_c,brk2_ext_c,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,breakertype,deviceid,breakerid]        
two_pbwmco_b_cList2=[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,brk1_int_c,brk1_ext_c,NULL,NULL,brk2_int_c,brk2_ext_c,NULL,NULL,NULL,NULL,NULL,NULL,breakertype,deviceid,breakerid]
two_pbwmco_c_aList2=[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,brk2_int_c,brk2_ext_c,NULL,NULL,NULL,NULL,NULL,NULL,brk1_int_c,brk1_ext_c,NULL,NULL,NULL,NULL,NULL,NULL,breakertype,deviceid,breakerid]
i=0
while (i<7):
    parseTwoPoleBreakers(7,8,two_pbwmco_a_bList2,i)
    breakerid+=1
    parseTwoPoleBreakers(9,10,two_pbwmco_b_cList2,i)
    breakerid+=1
    parseTwoPoleBreakers(11,12,two_pbwmco_c_aList2,i)
    breakerid+=1
    i+=1

您可以在函数内使用 global 关键字访问全局变量。即把

global brk1_int_c, brk1_ext_c, brk2_int_c, brk2_ext_c

在函数的顶部。

请记住,这被认为是糟糕的风格,例如参见this question。您可以考虑将这些变量包装到一个可以由您的函数执行的对象中。

您只需从函数的第一行取消注释(删除“#”)parseTwoPoleBreakers:

global brk1_int_c,brk1_ext_c,brk2_int_c,brk2_ext_c