2013-03-19 40 views
-1

我正在Python 2.7中編寫一個提示參數,以便用命令行界面導入我的數據文件。如何在python中重置輸入流?

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文件重置,因爲我的目標是轉換*的.py在* .EXE使用py2exe

+0

您的問題是什麼? – 2013-03-19 13:20:02

+1

把所有東西都放在'while'循環中,並且只在用戶輸入時退出(像'raw_input(「想退出?」)一樣的另外一個輸入''? – dmg 2013-03-19 13:20:38

+0

@Robᵩ:如何在python中重置輸入流? – 2013-03-19 13:33:03

回答

1

正如DJV所建議的那樣,我認爲這很簡單,就像在用戶遍歷所有選項之後在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):")