我有一個很大的宏
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "asa"
.Replacement.Text = "fsa"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "oriented"
.Replacement.Text = "das"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "ampere"
.Replacement.Text = "^sasd"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "wattage"
.Replacement.Text = "watts"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "balanced dit"
.Replacement.Text = "BD"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Opthamologist"
.Replacement.Text = "Eyes"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "goot health"
.Replacement.Text = "good health"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
我想從這個 vba 宏中創建一個查找替換字典
dictionary = {"asa":"fsa",
"oriented":"das",
"ampere":"^sasd",
"wattage":"watts",
"balanced dit":"BD",
"Opthamologist":"Eyes",
"goot health":"good health",}
整個 vba 宏應該被轉換成這個字典。
.text 和 .replacement 文本中的單詞應編入字典。
我需要一個 python 代碼來從 .TEXT 和 .Replacement Text 中獲取單詞并制作一個字典
"TEXT":"替換文本"
我希望我已經很好地解釋了我的問題。
uj5u.com熱心網友回復:
import re
with open('source.txt', 'r') as file:
inputText = file.readlines()
myDict = {}
for line in range(len(inputText)):
# Have a Text line
if inputText[line].lstrip().startswith('.Text'):
# Get the key with regular expression
key = re.search('"(. ?)"', inputText[line]).group(1)
value = re.search('"(. ?)"', inputText[line 1]).group(1)
myDict[key] = value
print(myDict)
輸出:
{'asa': 'fsa', 'oriented': 'das', 'ampere': '^sasd', 'wattage': 'watts', 'balanced dit': 'BD', 'Opthamologist': 'Eyes', 'goot health': 'good health'}
uj5u.com熱心網友回復:
使用正則運算式獲取引號內的所有內容。這將獲取所需輸出的鍵和值對。按步長為 2 的切片獲取所有鍵和值。
import re
text = # from file
regex = r'"([^"] )"'
matches = re.findall(regex, text)
out = dict(zip(matches[::2], matches[1::2]))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/486696.html
標籤:Python python-3.x vba 字典 查找替换
上一篇:VScode中配置Java環境
