ファイルチェックプログラムの初期段階でコーディングして作業するのが初めてです。 ファイルにタイトルに「正しい情報」が含まれているかどうかを確認するプログラムを作成しています。ファイル名の一部がディクショナリまたはリスト、Python 2.7に存在することを確認します。
タイトルのpart
が辞書またはリストの受け入れ可能な名前と一致するかどうかをチェックする方法が現在固まっています。
私のコードは以下の通りです。 3つ以下の部分を含むタイトルを挿入すると、エラーがスローされます。しかし、私が3 parts
のタイトルを入れた場合、parts
のどれもがリストや辞書の中のものと一致しない場合でも、私のプログラムはそのタイトルが正しいと主張します(それはそうではありません)。
私はif-statements
を知っているがために、すべてまたはさんの荒いですが、それは各part
ためif-statements
のトンを書く代わりに、今私が持っている最高のアイデアです。
誰かがコードを修正して、リストまたは辞書とのタイトルの一部をチェックして、その部分がリスト/辞書に存在することを確認できますか?
例(正しい)ファイル名は次のようになります。DJ_BR_UVT.xls
は、誤ったファイル名の例がある :DJ_BR_staford.xls
は *サイドノートとして、parts
または種は、学校は、イニシャルは、中に任意の順序にすることができファイル名。
def checkFilename(filename):
print 'filename = ' + filename
Parts = filename.split('_')
if len(Parts) != 3:
print "There are " + str(len(Parts)) + " that is an incorrect amount of info in file name. There should be 3 parts to your file name"
return
Part1 = Parts[0]
Part2 = Parts[1]
Part3 = Parts[2]
Species_Dictionary = {'Brown Rat':'BR', 'Black Rat':'BLR', 'Dwarf Rat':'DR', 'White Mouse':'GG', 'Human':'HS', 'Brown Mouse':'VK'}
School_List = ['UHJ', 'UMG', 'COL', 'UVT']
Initials_List = ['DM', 'DCM', 'YXAA', 'DJ']
Species_Check = 0
School_Check = 0
Initials_Check = 0
# supposed to check to see if each 'part' can be found in the Species_Dictionary
if Part1 or Part2 or Part3 in Species_Dictionary:
Species_Check = 1
print Species_Check
else:
print "Cannot find a valid species"
return
#check to see if any of the 'parts' can be found in the School-List
if Part1 or Part2 or Part3 in School_List:
School_Check = 1
else:
print "Cannot find valid school"
return
#Check if any of the 'parts' are in the Initials_List
if Part1 or Part2 or Part3 in Initials_List:
Initials_Check = 1
else:
print "Cannot find valid initials"
return
#If the previous 3 if-statements have been met, the file 'passes' and contains correct info
if Species_Check == 1 and School_Check == 1 and Initials_Check == 1:
print "Your file contains correct title information"
else:
print "Your file name does not contain the correct information"
return
を出力します。ファイル名は任意の順序にすることができます。種、イニシャル、スクールは常に1位、2位、3位にあるとは限りません。だからこそ、私は2つのリストと辞書を使って異なる 'パーツ'を実行したかったのです...ファイル名のどの部分にも種/イニシャル/スクールが含まれている可能性があるからです。特定の順序はありません。 – umgcoder