Python 如何在if语句外部声明变量
这是我的密码:Python 如何在if语句外部声明变量,python,Python,这是我的密码: last_state={} last_device={} last_substate={} with open("TESTFILE.csv") as f: reader = csv.reader(f, delimiter=',') for row in reader: device = row[0] state = row[1] model = row[2] substate=row[3]
last_state={}
last_device={}
last_substate={}
with open("TESTFILE.csv") as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
device = row[0]
state = row[1]
model = row[2]
substate=row[3]
#time = row[4]
if device in row:
if substate == 'Not joined' and model !='electric_meter':
if state == "Offline":
N_off = [device]
# print N_off, reader.line_num
if state == 'Online':
N_on = [device]
if N_off == N_on:
print device, reader.line_num
我试图比较这两个循环的设备ID,以便只使用满足所有条件的循环。我不知道为什么,但我得到了这个错误:
`if N_off == N_on:
NameError: name 'N_off' is not defined`
我不太熟悉Python和声明变量的工作原理,但我尝试全局声明它们,这给了我同样的错误。我理解这是因为我使用的
N_off
超出了它的范围,但我不知道如何纠正 假设您希望或需要N_off
成为全局文件,则需要导入包含N_off
全局声明的文件,并在此文件中将其用作文件名.N_off
。在包含声明的文件中,不需要在其前面加上文件名。请注意,您(通常)不需要在python中声明变量。全局变量是一个例外
示例:
其他文件
CurrentFile
请看一下代码的这一部分:
if state == "Offline":
N_off = [device]
if state == 'Online':
N_on = [device]
if N_off == N_on:
print device, reader.line_num
如您所见,N\u off
仅在state==“Offline”
时定义,而N\u on
仅在state==“Online”时定义
因此,当您在最后一个if
语句中比较它们时,Python只定义了其中一个,因此只知道其中一个
您只需在程序中的某个地方进行初始化,以确保它们都已定义。您已在
内部定义了N_off
和N_on
如果
语句且只有一个或另一个可以为真并执行,请尝试以下操作:
last_state={}
last_device={}
last_substate={}
with open("TESTFILE.csv") as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
device = row[0]
state = row[1]
model = row[2]
substate=row[3]
#time = row[4]
if device in row:
if substate == 'Not joined' and model !='electric_meter':
N_off = None
N_on = None
if state == "Offline":
N_off = [device]
# print N_off, reader.line_num
if state == 'Online':
N_on = [device]
if N_off == N_on:
print device, reader.line_num
但我看不出其中的逻辑:
if state == "Offline":
N_off = [device]
# print N_off, reader.line_num
if state == 'Online':
N_on = [device]
if N_off == N_on:
print device, reader.line_num
因为:
if N_off == N_on:
print device, reader.line_num
将永远不会执行,因为状态不能同时处于打开和关闭状态。假设您希望在所有设备上循环,并希望检查连续行是否具有与“脱机”和“联机”相同的设备值 您可以使用下面的代码,基本上在检查变量是否相等时,我添加了额外的条件来检查变量是否初始化。如果它们没有初始化,我假设您的条件不会得到满足
last_state={}
last_device={}
last_substate={}
#reader = [('device','Offline','model','Not joined')]
# Instead of file reading from list
reader = [('device','Offline','model','Not joined'),('device','Online','model','Not joined')]
for row in reader:
device = row[0]
state = row[1]
model = row[2]
substate=row[3]
#time = row[4]
if device in row:
if substate == 'Not joined' and model !='electric_meter':
print(state)
if state == "Offline":
N_off = [device]
if state == 'Online':
N_on = [device]
# added condition to check if this variables are initialized or not.
if 'N_on' in locals() and 'N_off' in locals() and N_off == N_on:
print "Check passed"
在上面声明
如果设备在第行:
您是如何尝试全局声明它的?您的代码是否在函数中?你第一次在哪里声明了N\u off
?我很想在之后声明它,如果行中的设备
带有'N\u off=None',然后我得到了错误:'NoneType'对象没有属性'\uu getitem\uuuuuu'
在@Rekesh建议的地点声明它,并从N\u off和N\u on中删除[]似乎起到了作用,虽然我不能说我以后能理解。无论哪种方式,感谢所有人的帮助一台设备重复多次,状态可以从在线变为离线。我试图用该行执行的是,如果N_Off[device]==N_on[device]然后打印,我只希望在设备不稳定时打印。有没有其他方法可以做到这一点。
if N_off == N_on:
print device, reader.line_num
last_state={}
last_device={}
last_substate={}
#reader = [('device','Offline','model','Not joined')]
# Instead of file reading from list
reader = [('device','Offline','model','Not joined'),('device','Online','model','Not joined')]
for row in reader:
device = row[0]
state = row[1]
model = row[2]
substate=row[3]
#time = row[4]
if device in row:
if substate == 'Not joined' and model !='electric_meter':
print(state)
if state == "Offline":
N_off = [device]
if state == 'Online':
N_on = [device]
# added condition to check if this variables are initialized or not.
if 'N_on' in locals() and 'N_off' in locals() and N_off == N_on:
print "Check passed"