[英]Python: replacing multiple words in a text file from a dictionary
我無法弄清楚我哪里出錯了。 因此,我需要隨機替換單詞並將其重新寫入文本文件,直到對其他人不再有意義。 我選擇了一些單詞來測試它,並編寫了以下代碼,目前無法正常工作:
# A program to read a file and replace words until it is no longer understandable
word_replacement = {'Python':'Silly Snake', 'programming':'snake charming', 'system':'table', 'systems':'tables', 'language':'spell', 'languages':'spells', 'code':'snake', 'interpreter':'charmer'}
main = open("INF108.txt", 'r+')
words = main.read().split()
main.close()
for x in word_replacement:
for y in words:
if word_replacement[x][0]==y:
y==x[1]
text = " ".join(words)
print text
new_main = open("INF108.txt", 'w')
new_main.write(text)
new_main.close()
這是文件中的文字:
Python是一種廣泛使用的通用高級編程語言。 它的設計理念強調代碼可讀性,其語法允許程序員用比C ++或Java等語言更少的代碼行表達概念。 該語言提供了用於在小規模和大規模上實現清晰程序的構造.Python支持多種編程范例,包括面向對象,命令式和函數式編程或程序樣式。 它具有動態類型系統和自動內存管理功能,並具有大型全面的標准庫.Python解釋器可在許多操作系統上安裝,允許在各種系統上執行Python代碼。 使用第三方工具,如Py2exe或Pyinstaller,可以將Python代碼打包到一些最流行的操作系統的獨立可執行程序中,允許分發基於Python的軟件,以便在這些環境中使用而無需安裝一個Python解釋器。
我已經嘗試了一些方法,但作為Python的新手,這是一個猜測的問題,並且最近兩天花在網上進行研究,但我發現的大部分答案要么太復雜,我不能理解,或是特定於該人的代碼,並沒有幫助我。
好的 ,讓我們一步一步來。
main = open("INF108.txt", 'r+')
words = main.read().split()
main.close()
最好在這里使用with
語句。 此外, r
是默認模式。 從而:
with open("INF108.txt") as main:
words = main.read().split()
使用with
將使main.close()
在此塊結束時自動為您調用; 你也應該為最后的文件寫做同樣的事情。
現在為主要位:
for x in word_replacement:
for y in words:
if word_replacement[x][0]==y:
y==x[1]
這個小部分包含了幾個誤解:
for x in word_replacement
)只給出了它的鍵 。 因此,當您想稍后進行比較時,您應該檢查if word_replacement[x] == y
。 在那上面做[0]
只會給你替換的第一個字母 。 y in word_replacement
檢查它們是否在字典y in word_replacement
。 y == x[1]
在兩個方面是錯誤的。 首先,你可能意味着在那里分配 y
,而不是比較 (即y = x[1]
- 注意單個=
符號)。 其次,分配給循環變量甚至不能做你想要的。 y
將在下一次循環中被新值覆蓋,並且words
數據將根本不會被更改。 你想要做的是創建一個可能被替換的單詞的新列表,如下所示:
replaced = []
for y in words:
if y in word_replacement:
replaced.append(word_replacement[y])
else:
replaced.append(y)
text = ' '.join(replaced)
現在讓我們做一些改進。 字典有一個方便的get
方法,可以讓你在鍵存在時得到一個值,如果沒有則可以得到默認值。 如果我們只使用單詞本身作為默認值,我們會得到一個漂亮的減少:
replaced = []
for y in words:
replacement = word_replacement.get(y, y)
replaced.append(replacement)
text = ' '.join(replaced)
您可以將其轉變為單行列表理解 :
text = ' '.join(word_replacement.get(y, y) for y in words)
現在我們已經完成了。
看起來你想要這樣的東西作為嵌套循環中的if語句:
if x==y:
y=word_replacement[x]
循環遍歷字典時,會獲得其鍵,而不是鍵值對:
>>> mydict={'Python':'Silly Snake', 'programming':'snake charming', 'system':'table'}
>>> for i in mydict:
... print i
Python
programming
system
然后,您可以使用mydict[i]
獲取值。
但是,這並不常用,因為賦值給y
並不會改變words
元素。 您可以遍歷其索引而不是元素以分配給當前元素:
for x in word_replacement:
for y in range(len(words)):
if x==words[y]:
words[y]=word_replacement[x]
你的問題可能在這里:
if word_replacement[x][0]==y:
這是實際發生的一個小例子,可能不是你想要的:
w = {"Hello": "World", "Python": "Awesome"}
print w["Hello"]
print w["Hello"][0]
哪個應該導致:
"World"
"W"
您應該能夠從這里弄清楚如何更正代碼。
你以錯誤的方式使用word_replacement
(這是一個字典)。 您應該將for循環更改為以下內容:
for y in words:
if y in word_replacement:
words[words.index(y)] = word_replacement[y]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.