當前位置:成語大全網 - 英語詞典 - Python 根據字典的內容替換文本的裏的字符串

Python 根據字典的內容替換文本的裏的字符串

我覺得妳好想想多了。即然全文替換。哪麽妳只需要遍歷妳的dict

a_dict = {'apple':'1','tree':'2','123456':'3'}

input_file = open(r'd:\test_body.txt',"r").read()();

for key,value in a_dict.items():

input_file=input_file.replace(key,value);

這樣子不行嗎?