【发布时间】:2020-06-15 12:56:44
【问题描述】:
我对 python 语言比较陌生。我想从文本文件中读取数据(文本文件中的多行),然后使用从文本文件中读取的数据来执行字典函数。
def readCmd():
f = open('cmd.txt', "r")
line = f.readline()
for line in f:
print (line)
time.sleep(1)
return str(line)
f.close()
def zero():
print( "Hi 0")
def one():
print( "Test 1")
def two():
print( "end 2")
def num_to_func_to_str(argument):
switcher = {
"Hi": zero,
"test": one,
"end": two,
}
print(switcher.get(argument,"Please enter only 'Hi', 'test' and 'end'"))
def main():
#readCmd()
while 1 :
time.sleep(0.5)
num_to_func_to_str(readCmd())
if __name__ == "__main__":
main()
上面的代码是我试过的代码。它只显示了第二行,而不是进入字典(切换器)条件。此代码跳至print(switcher.get(argument,"Please enter only 'Hi', 'test' and 'end'"))
文本文件中的数据如下。
start
Hi
test
Hi
test
Hi
test
Hi
test
Hi
test
Hi
test
end
谁能建议我如何解决这个问题? 谢谢
【问题讨论】:
标签: python python-3.x