ich eine Datei als Wörterbuch Wort habe:Wie die Worte nicht im Wörterbuch überspringen
water=45
melon=8
apple=35
pineapple=67
I=43
to=90
eat=12
tastes=100
sweet=21
it=80
watermelon=98
want=70
juice=88
und ich habe eine andere Datei mit dem folgenden Text:
I want to eat banana and watermelon
I want drink juice purple and pineapple
I ausgegeben werden soll:
43, 70, 90, 12, 98
43, 70, 88, 67
Jedes Wort, das nicht im Wörterbuch vorhanden ist, wird übersprungen.
Dies ist, was ich bisher:
import re
f = open(r'C:\Users\dinesh_pundkar\Desktop\val.txt','r')
val_dict = {}
for line in f:
k, v = line.strip().split('=')
val_dict[k.strip()] = v.strip()
f.close()
h = open(r'C:\Users\dinesh_pundkar\Desktop\str_txt.txt','r')
str_list = []
for line in h:
str_list.append(str(line).strip())
tmp_str = ''
for val in str_list:
tmp_str = val
for k in val_dict.keys():
if k in val:
replace_str = str(val_dict[k]).strip() + ","
tmp_str= re.sub(r'\b{0}\b'.format(k),replace_str,tmp_str,flags=re.IGNORECASE)
tmp_str = tmp_str.strip(",")
print val, " = ", tmp_str
tmp_str = ''
Ausgang:
43, 70, 90, 12, banana and 98
43, 70, drink 88, purple and 67
Ich habe versucht, den Code auszuführen, aber die Ausgabe ist nicht im Ausführen ..., das ist sein Code [Link] (http://www.tutorialspoint.com/execute_python_online.php?PID=0Bw_CjBb95KQMZDI2MDhaV2FZZFE) –
@RiskaNanda Du hast die '...:' 's kopiert, die IPython generiert hat; Wenn Sie sie entfernen, funktioniert es. :) –
Ich habe es gelöscht, aber die Ergebnisse bleiben gleich. [Link] (http://www.tutorialspoint.com/execute_python_online.php?PID=0Bw_CjBb95KQMczl4VXJXTDVYa0U) –