我有一个很大的宏
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文本中的单词,并制作一个字典
“文本”:“ReplacementText”
我希望我已经很好地解释了我的问题。
发布于 2022-06-06 11:52:57
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'}
发布于 2022-06-06 12:38:39
使用正则表达式获取引号中的所有内容。这将获取希望输出的键和值对。按步骤2的切片获取所有键和值。
import re
text = # from file
regex = r'"([^"]+)"'
matches = re.findall(regex, text)
out = dict(zip(matches[::2], matches[1::2]))
https://stackoverflow.com/questions/72517126
复制相似问题