修正.rtf fi中给定单词的Python脚本

2024-09-29 18:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的新手,目前正在研究它。我有这样的语法:

def main():
#with auto \1 & \2 on replacement
search_replace_mixed = {'eousisienl':'consistent'}
#no auto variable on replacement
search_replace_exact = {'eousisienl':'consistent'}

这将纠正一个拼写错误的单词,我不知道下一步是什么。我的源文档是.RTF,我需要一个python脚本来纠正上述语法中的一个单词。或者你有更好的方法。你能帮助我吗?在


Tags: noautosearchmainondefwith语法
1条回答
网友
1楼 · 发布于 2024-09-29 18:30:03

有几种方法可以帮你解决这个问题。如果你只是想换一组拼写错误的单词,你可以这样拼凑起来:

filename = 'words.rtf'

search_replace_exact = {'eousisienl': 'consistent'}

with open(filename, 'r+') as file:

    text = file.read().split(' ')

    for index, word in enumerate(text):
        if word in search_replace_exact:
            text[index] = search_replace_exact[word]

    corrected_text = ''

    for element in text:
        corrected_text += element + ' '

    file.seek(0)
    file.truncate()
    file.write(corrected_text)

这将搜索您的RTF文档并替换您的serach_replace_exact字典中特别列出的内容。在

相关问题 更多 >

    热门问题