2016-03-30 11 views
0

ファイルチェックプログラムの初期段階でコーディングして作業するのが初めてです。 ファイルにタイトルに「正しい情報」が含まれているかどうかを確認するプログラムを作成しています。ファイル名の一部がディクショナリまたはリスト、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 
+0

を出力します。ファイル名は任意の順序にすることができます。種、イニシャル、スクールは常に1位、2位、3位にあるとは限りません。だからこそ、私は2つのリストと辞書を使って異なる 'パーツ'を実行したかったのです...ファイル名のどの部分にも種/イニシャル/スクールが含まれている可能性があるからです。特定の順序はありません。 – umgcoder

答えて

1

条件if Part1 or Part2 or Part3 in Species_Dictionary:はあなたの考えをしません。

ファイル名が、その後partsDJBRUVT.xlsなりますDJ_BR_UVT.xlsある場合。拡張機能を削除する必要があります。

PARTS1 = ('BR','BLR','DR','GG','HS','VK') 
PARTS2 = ('UHJ', 'UMG', 'COL', 'UVT') 
PARTS3 = ('DM', 'DCM', 'YXAA', 'DJ') 
def checkFilename(filename): 
    f = filename.split('.')[0] # this removes the extension 
    parts = f.split('_') 
    nb1, nb2, nb3 = 0, 0, 0 
    for p in parts: 
    if p in PARTS1: nb1 += 1 
    if p in PARTS2: nb2 += 1 
    if p in PARTS3: nb3 += 1 
    return nb1 == 1 and nb2 == 1 and nb3 == 1 

print (checkFilename("DJ_BR_UVT.xls")) 
print (checkFilename("DJ_BR_staford.xls")) 

これは明確化のため

True 
False 
+0

すてきな答え...パーツが本当にどんな順序でもよいと仮定すると(ファイルパスに_複数のピリオドがない、または完全に指定されていない可能性があります:P)、これは仕様が提供された:) –

関連する問題