在microsoft office word文件Python中用字母替换符号

2024-09-29 23:33:04 发布

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

我有一个文本文件 文本,例如izvo|enje ovla{}ujem Milivojevi} garae ~lan。你知道吗

通常我在Microsoft office wordFindReplace选项中使用每个符号来更改字母。你知道吗

我想得到文本izvodenje ovlascujem Milivojevic garaze clan

所以呢

  • 符号|更改为c
  • 符号{更改为s
  • 符号~更改为c 等等。你知道吗

我该怎么做?我想自动化这个过程,将代码保存在文件中,并像脚本一样使用这些代码。 我想用Python,(我真的是初学者)


Tags: 代码文本符号replacemicrosoftoffice文本文件lan
3条回答
if __name__ == '__main__':

with open('file.txt', 'r') as f:
    sentence = f.read()
correct = sentence.replace('|', 'c')
print(correct)

印刷品:

izvocenje ovla{}ujem Milivojevi} gara`e ~lan

其余的都很琐碎,所以这是一个自学python的好机会

将字符串存储在var中并使用replace('old str','new str')函数

string = "izvo|enje ovla{}ujem Milivojevi} gara`e ~lan"
print string.replace("|","d").replace("{", "s").replace("}", "c").replace("`", "z").replace("~", "c")

更简单的方法是使用如下方法替换:

text= "one two three"
text = text.replace("two", "2")

对于多个替换,我建议如下循环:

text= "one two three"
replaceable = {'one': '1', 'two': '2', 'three': '3'}
for string, new_string in replaceable.items(): 
    text = text.replace(string, new_string)

相关问题 更多 >

    热门问题