如何在python中重置输入流?

时间:2013-03-19 13:17:47

标签: python input command-prompt

我在Python 2.7中编写一个提示参数,以便使用命令行界面导入我的数据文件。

REFERENCE = raw_input("Reference (*.shp):")
SEGMENTED = raw_input("Segmented (*.shp):")
METHOD = raw_input("Method (ke, pu, clinton):")
if METHOD != "ke" and METHOD != "pu" and METHOD != "clinton":
    raise ValueError("%s is not a valid method" % METHOD)
if METHOD == "ke" or METHOD == "clinton":
    THRESHOLD = input("Threshold (0.0 - 1.0):")
    if not check_threshold(THRESHOLD):
        raise AccuracyException("Threshold of %s is not valid" % THRESHOLD)
else:
    THRESHOLD = None
SEP = raw_input("Sep:")
if SEP != "space" and SEP != "tab" and SEP != "comma" and SEP != "colon" and SEP != "semicolon" and SEP != "hyphen" and SEP != "dot":
    raise ValueError("%s is not valid" % SEP)
HEADER = raw_input("Header (True/False):")
if HEADER.strip() != "True" and HEADER.strip() != "False":
    raise ValueError("%s is not valid" % HEADER)
# output 
OUTPUT = raw_input("Output (*.txt):")

加载后我希望从头开始重置以便导入新数据而不重新加载* .py文件,因为我的目标是使用py2exe转换* .exe中的* .py

1 个答案:

答案 0 :(得分:1)

正如DJV建议的那样,我认为这就像在一个while循环中包装你的脚本以便在用户完成所有选项之后继续在while块的顶部一样简单

while True:
  REFERENCE = raw_input("Reference (*.shp):")
  SEGMENTED = raw_input("Segmented (*.shp):")
  METHOD = raw_input("Method (ke, pu, clinton):")
  if METHOD != "ke" and METHOD != "pu" and METHOD != "clinton":
    raise ValueError("%s is not a valid method" % METHOD)
  if METHOD == "ke" or METHOD == "clinton":
    THRESHOLD = input("Threshold (0.0 - 1.0):")
    if not check_threshold(THRESHOLD):
        raise AccuracyException("Threshold of %s is not valid" % THRESHOLD)
  else:
    THRESHOLD = None
  SEP = raw_input("Sep:")
  if SEP != "space" and SEP != "tab" and SEP != "comma" and SEP != "colon" and SEP != "semicolon" and SEP != "hyphen" and SEP != "dot":
    raise ValueError("%s is not valid" % SEP)
  HEADER = raw_input("Header (True/False):")
  if HEADER.strip() != "True" and HEADER.strip() != "False":
    raise ValueError("%s is not valid" % HEADER)
  # output 
  OUTPUT = raw_input("Output (*.txt):")
相关问题