我有一個python程序,我傳入一個文件,我讀這個文件,然後拆分冒號的行。 然後我打印這兩個部分,做一些檢查,並將其傳遞給一個函數,如果匹配它打印出匹配,然後返回。但我無法弄清楚如何,然後讓我在文件中的下一行,該計劃目前只是不斷與旁邊一遍又一遍地這條線Python文件迭代和嵌套for循環
with open(myfile,'r') as hf:
for l in hf:
part1 = l.split(":")[0].strip()
part2 = l.split(":")[1].strip()
print part1
print part2
print "**************"
for file in filenames:
print "Starting " + file
if ".txt" in file or ".lst" in file:
file = os.path.join(mypath, file)
with open(file,'r') as f:
for line in f:
for word in line.split():
ThenWord(part2,word)
我曾嘗試突破,繼續和別的,沿()但我似乎無法得到它的工作,或者它在錯誤的地方。 我怎麼會得到從打開的文件的下一行,然後開始for循環再次在結腸分割,3號線和4
編輯: 我在2個符有加,但我儘量的文件和匹配單詞(對於文件中的文件名)只讀取第一個文件,然後從myfile移至下一行。
with open(myfile,'r') as hf:
for l in hf:
part1 = l.split(":")[0].strip()
part2 = l.split(":")[1].strip()
print part1
print part2
print "**************"
for file in filenames:
print "Starting " + file
if ".txt" in file or ".lst" in file:
file = os.path.join(mypath, file)
with open(file,'r') as f:
for line in f:
for word in line.split():
ThenWord(part2,word)
break
break
def ThenWord(salt,word):
salted = salt + word
m = hashlib.md5()
m.update(salted)
if m.hexdigest() == hash:
print "************ " + hash + " ************"
print "******* Enough said - " + word + " ******* "
return
我想它,以便一旦找到匹配,它不通過掃描文件名中的所有其他文件移動到文件(MYFILE)在未來的哈希值。
你可以用'for line in f.readlines()'來嘗試。 – SunilT
f中的行是該程序的一個不同部分,我正嘗試將下一行 '與open(myfile,'r')作爲hf: for hf:' – Hoohaee
如果註釋掉for循環中的大部分代碼? – Gribouillis