所以,这里是函数中的一大块代码:
print('Printing current values ...')
for key, modbusObject in jsonDict.items():
print("Object {} contains value {} and timestamp {}".format(key, modbusObject.value, modbusObject.timestamp))
print('Writing data for loop number '+str(i)+' ...')
# server.send_message_to_all(jsonDict)
#print('ho ho ho')
i += 1
cmd = raw_input('Change json data (y/n) ? --type \'exit\' to quit-- : ')
所有这些都嵌套在try ... except语句中。无论我代替i + = 1 - > for exemple print('ho ho ho')被拒绝为'SyntaxError:invalid syntax'。
我已经被困在这2天了,无法弄清楚这里发生了什么导致这个错误......
编辑:根据要求提供更多代码:
def new_client(client, server):
print("New client connected and was given id %d" % client['id'])
server.send_message_to_all('hello foo')
# global jsonDict
# global i
while True:
print('Currently connected clients : ')
try:
for key, modbusObject in jsonDict.items():
for case in switch(key):
if case("coil1000"):
if(i % 2 == 0):
modbusObject.value = 1
else:
modbusObject.value = 0
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("coil1001"):
if(i % 2 == 0):
modbusObject.value = 0
else:
modbusObject.value = 1
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("coil1002"):
if(i % 2 == 0):
modbusObject.value = 1
else:
modbusObject.value = 0
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("coil1008"):
if(i % 2 == 0):
modbusObject.value = 0
else:
modbusObject.value = 1
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("coil1009"):
if(i % 2 == 0):
modbusObject.value = 1
else:
modbusObject.value = 0
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("coil1010"):
if(i % 2 == 0):
modbusObject.value = 0
else:
modbusObject.value = 1
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("coil1011"):
if(i % 2 == 0):
modbusObject.value = 1
else:
modbusObject.value = 0
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("inh1000"):
if(i % 2 == 0):
modbusObject.value = 127
else:
modbusObject.value = 365
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("inh1001"):
if(i % 2 == 0):
modbusObject.value = 8350
else:
modbusObject.value = 4633
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("inh1002"):
if(i % 2 == 0):
modbusObject.value = 20
else:
modbusObject.value = 15
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("inh1003"):
if(i % 2 == 0):
modbusObject.value = 40
else:
modbusObject.value = 10
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("inh1025"):
if(i % 2 == 0):
modbusObject.value = 6
else:
modbusObject.value = 3
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("inh1034"):
if(i % 2 == 0):
modbusObject.value = 0
else:
modbusObject.value = 5
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("inh1035"):
if(i % 2 == 0):
modbusObject.value = 162
else:
modbusObject.value = 50
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("inh1036"):
if(i % 2 == 0):
modbusObject.value = 40
else:
modbusObject.value = 20
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("inh1037"):
if(i % 2 == 0):
modbusObject.value = 50
else:
modbusObject.value = 25
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1000"):
if(i % 2 == 0):
modbusObject.value = 0
else:
modbusObject.value = 50
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1001"):
if(i % 2 == 0):
modbusObject.value = 100
else:
modbusObject.value = 0
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1002"):
if(i % 2 == 0):
modbusObject.value = 40
else:
modbusObject.value = 37
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1014"):
if(i % 2 == 0):
modbusObject.value = 350
else:
modbusObject.value = 221
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1015"):
if(i % 2 == 0):
modbusObject.value = 200
else:
modbusObject.value = 16
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1018"):
if(i % 2 == 0):
modbusObject.value = 1000
else:
modbusObject.value = 1246
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1021"):
if(i % 2 == 0):
modbusObject.value = 2000
else:
modbusObject.value = 1432
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1086"):
if(i % 2 == 0):
modbusObject.value = 0
else:
modbusObject.value = 1
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1094"):
if(i % 2 == 0):
modbusObject.value = 1
else:
modbusObject.value = 0
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1102"):
if(i % 2 == 0):
modbusObject.value = 3000
else:
modbusObject.value = 50
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
break
if case("in1104"):
if(i % 2 == 0):
modbusObject.value = 100
else:
modbusObject.value = 3
modbusObject.timestamp = dumps(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"), default=json_serial)
if case(''):
pass
print('Printing current values ...')
for key, modbusObject in jsonDict.items():
print("Object {} contains value {} and timestamp {}".format(key, modbusObject.value, modbusObject.timestamp))
print('Writing data for loop number '+str(i)+' ...')
# server.send_message_to_all(jsonDict)
#print('ho ho ho')
i += 1
cmd = raw_input('Change json data (y/n) ? --type \'exit\' to quit-- : ')
if cmd != '':
if cmd.lower() == 'exit':
exit()
elif cmd.lower() == 'y':
i += 1
else:
pass
except Exception as e:
traceback.print_exc()
finally:
time.sleep(8)
注意:
print("New client connected and was given id %d" % client['id'])
server.send_message_to_all('hello foo')
和
while True:
块正确缩进,这只是它们看起来不是复制/粘贴的结果。
我使用的开关...案例替代来自here
答案 0 :(得分:0)
将缩进转换为空格后问题得以解决。谢谢大家的帮助。