フォルダー内のすべてのテキスト ファイル (* .txt)または 1 つのテキスト ファイルを読み込み、データ形式が正しいかどうかを確認し ( get_parse
and check_header
)、データを処理するために、このステートメントを Python で記述しました (プログラムを実行可能ファイルに変換するため)。. データ形式が正しくない場合、プログラムは再起動を要求します ( doit = raw_input("Continue (Y/N)?[Y]: "
)。最初のケース (*.txt) では、データ形式が正しくありません (区切り文字 '.' と最初の行のヘッダーの存在)。プログラムはエラーを報告し、(Y が押された場合) から再起動しINPUT = raw_input("Input (*.txt): ")
ます。2 番目のケース (単一のテキスト ファイルをロードする) では、データ形式が正しくない場合、プログラムは終了します。
単一のロード テキスト ファイルについても、ユーザーがプログラムを再起動する可能性を選択したいと思います。INPUT = raw_input("Input (*.txt): )
while True:
INPUT = raw_input("Input (*.txt): ")
if os.path.split(INPUT)[1] == "*.txt":
file_list = glob.glob(os.path.join(os.path.split(INPUT)[0], '*.txt'))
for file in file_list:
file_head, file_tail = os.path.split(file)
file_root, file_suffix = os.path.splitext(file_tail)
try:
parse = get_parse(file)
except Exception:
print ValueError("%s has delimiter type not valid" % file_tail)
raw_input("press any key to exit")
print "Exiting..."
break
if check_header(file):
print ValueError("%s has an Header" % file_tail)
raw_input("press any key to exit")
print "Exiting..."
break
# do other stuff
doit = raw_input("Continue (Y/N)?[Y]: ")
if doit == 'N'or doit == 'n':
print "Exiting....."
break
else:
try:
parse = get_parse(INPUT)
except Exception:
print ValueError("Delimiter type not valid")
raw_input("press any key to exit")
print "Exiting..."
break
if check_header(INPUT):
print ValueError("Header is not valid")
raw_input("press any key to exit")
print "Exiting..."
break
# do other stuff
doit = raw_input("Continue (Y/N)?[Y]: ")
if doit == 'N'or doit == 'n':
print "Exiting....."
break
2 番目のケースでは、Windows のコマンド プロンプトを使用すると、テキスト ファイルが正しくありません。
Input (*.txt): C:\test.txt
Delimiter type not valid
press any key to exit
Exiting.....
C:\Users\>
最初のケース (*.txt)
Input (*.txt): C:\*.txt
Area1_1.txt has delimiter type not valid
press any key to exit
Exiting...
Continue (Y/N)?[Y]: