全局变量传递到函数参数列表

时间:2016-04-23 20:38:34

标签: python function variables parameters global

我的parseTwoPoleBreakers功能有问题。我正在尝试将一个列表inputList输入到该函数中,因此我知道在该列表中放置brk1_int_c,brk1_ext_c,brk2_int_c,brk2_ext_c的位置。运行NameError: global name 'brk1_int_c' is not defined下面的脚本时出现以下错误。我很确定我使用行global brk1_int_c,brk1_ext_c,brk2_int_c,brk2_ext_c定义我的变量,但我可能错了,所以关于如何定义这些变量并进入列表inputList的任何输入都将非常感激。

#purpose2: To read in the 21 2 pole breaker panel data sheets, and parse them into the correct format, so they can be imported into the database.


import numpy as np
import pandas as pd



# 1. input data and store in data frame
with pd.ExcelFile("C:/Users/Documents/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


# 2bc. FDC Feed Braeker "List"
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
    print aRowNum
    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=[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,a_int_c,a_ext_c,NULL,NULL,b_int_c,b_ext_c,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,breakertype,deviceid,breakerid]
    two_pbwmco_a_b=inputList
    breakerid+=1
    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 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)
    parseTwoPoleBreakers(9,10,two_pbwmco_b_cList2,i)
    parseTwoPoleBreakers(11,12,two_pbwmco_c_aList2,i)
    i+=1

1 个答案:

答案 0 :(得分:1)

如果它应该是“全局的”,你可能必须先在文件中直接定义它,而不是在函数中。

尝试在代码文件的开头添加类似内容:

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