2016-04-09 17 views
0

に意味一致する単語を印刷するには英語の辞書を含むテキストファイルを読む:あなたは意味がある見ることができるように私はこのように記載されている言葉で英語の辞書を持っているテキストファイルを持っているのpython

Zymome (n.) A glutinous substance, insoluble in alcohol, resembling legumin;   -- 
now called vegetable fibrin, vegetable albumin, or gluten casein. 

Zymometer (n.) Alt. of Zymosimeter 

Zymophyte (n.) A bacteroid ferment. 

複数行の誰でもテキストファイルの入力で単語を検索し、その単語の対応する意味を表示するプログラムを手伝ってもらえますか?私が試した

コード:

x=raw_input("Enter word: ") 
with open('e:\\Python27\\My programs\\dictionary.txt') as file:  
    data = file.readlines() 
    for line in data: 
     if x in line: 
      print line 

ありがとう!

+0

あなたがこれまでに試してみましたか?あなたに何らかの努力を払い、あなたが立ち往生したときに私たちは助けます。 – mwm314

+0

何が問題なのですか?コードが機能するはずです。 –

答えて

1

私は、これはあなたが欲しいものだと思う:

x = raw_input("Enter word: ") 
with open('dictionary.txt') as file: 
    data = file.read() 

x_pos = data.find(x) 
meaning= None 
if x_pos == 0 or (x_pos != -1 and data[x_pos - 1] == '\n' and data[x_pos - 2] == '\n'): 
    i = x_pos 
    while i < len(data): 
     if data[i] == '\n' and i > 0 and data[i - 1] == '\n': 
      break 
     meaning += data[i] 
     i += 1 

print meaning if meaning else "Not found" 

または正規表現ベースのソリューション:

import re 


x = raw_input("Enter word: ").strip() 
with open('d.txt') as file: 
    data = '\n\n' + file.read() + '\n\n' 

pattern = r'\n\n(' + x + r' .*?)\n\n' 
pattern_compiled = re.compile(pattern, re.DOTALL) 

res = pattern_compiled.search(data) 
if res: 
    print(res.group(1)) 
else: 
    print('Not found') 
関連する問題