2015-11-11 98 views
2

我有兩個文件:獲取行號

文件1:

military troop deployment number need 

文件2:

foreign 1242 
military 23020 
firing 03848 
troop 2939 
number 0032 
dog 1234 
cat 12030 
need w1212 

我想從文件中讀取1行並打印它們在file2中的文字和行號。

我的輸出應該是這樣的:

military 2, troop 4, deployment <does not exist>, number 5, need 8 

我試過代碼:

words= 'military troop deployment number need' 
sent = words.split() 
print sent 

with open("file2","r") as f1: 
    for line_num,line in enumerate(f1): 
     if any([word in line for word in sent]): 
      print line_num, line 

這是打印所有行中,這些話都是。除此之外,它還打印了像軍事前,不必要的等字樣......我只需要那些確切的單詞和他們的行號。請幫助

回答

2

您正在打印錯誤的東西。你想要打印不是整行的單詞。此外,如果您使用any,則不知道哪個詞匹配。

這裏有2種方法。第一個不檢測空條目。

words= 'military troop deployment number need' 
sent = words.split() 

matched = [] 
with open("file2","r") as f1: 
    for i, line in enumerate(f1): 
     for word in sent: 
      if word in line: 
       matched.append('%s %d' % (word, i + 1)) 

print ', '.join(matched) 

輸出:

military 2, troop 4, number 5, need 8 

如果你要打印的空項爲好。

words= 'military troop deployment number need' 
sent = words.split() 

linenos = {} 

with open("file2","r") as f1: 
    for i, line in enumerate(f1): 
     for word in sent: 
      if word in line: 
       linenos[word] = i + 1 

matched2 = [] 
for word in sent: 
    if word in linenos: 
     matched2.append('%s %d' % (word, linenos[word])) 
    else: 
     matched2.append('%s <does not exist>' % word) 
print ', '.join(matched2) 

輸出:

military 2, troop 4, deployment <does not exist>, number 5, need 8 

爲了處理一個字並打印第一線的多個實例。

words= 'military troop deployment number need' 
sent = words.split() 
linenos = {} 

with open("file2", "r") as f1: 
    for i, line in enumerate(f1): 
     for word in sent: 
      if word in line: 
       if word in linenos: 
        linenos[word].append(i + 1) 
       else: 
        linenos[word] = [i + 1] 

matched2 = [] 
for word in sent: 
    if word in linenos: 
     matched2.append('%s %r' % (word, linenos[word][0])) 
    else: 
     matched2.append('%s <does not exist>' % word) 

print ', '.join(matched2) 

輸出與前面的例子相同。

+0

它有很大的幫助。非常感謝你:)如果我只是想讓它打印最早的行號,該怎麼辦?就像軍事發生在10個地方一樣。如果我只想要第一個地方的行號呢? –

+0

也許這個詞典可以包含一個單詞出現的所有行號的列表。那麼如果你只想要第一個你只使用列表的第0個元素。 –

+0

好的。我會嘗試。如果可能的話,如果你可以添加這行代碼,那也會很棒。我只是在學習。沒有壓力雖然:) –