我想給出編碼指令'ae | ea | s3',其中a被e替換,e被替換爲a,s被替換爲3 for相同的字符串。但我有問題寫程序插入模式chr替換爲chr1 | chr2替換爲chr3 | chr4替換爲chr5。對於上面的模式chr,chr2和chr4是我想要由chr1,chr3,chr5和「|」替換的字符串中的字符。代表「和」。我搜索谷歌,但無法找到有關上述問題的任何提示。獲取模式ae | ea | s3替換python中的字符串
def encode(filename):
path = "C:\Users\user\Desktop"
basename = "assgn2part1.txt"
filename = path + "\\" + basename
#file = open(filename, "rt")
f = open(filename)
contents = f.read()
print contents ,"\n"
chars = ["a","b","e","3","d","r","g","j","h","i"]
newcontents = contents.translate(string.maketrans("aes","ea3"))
print newcontents
我得到的答案是:
This is a message
Thi3 i3 e ma33ega
這並給出結果,但我想插入的定義本身
encode("ae|ea|s3","C:\Users\user\Desktop\\assgn2part1.txt")
模式給我的結果,我應該能夠用另一種類型的模式替代它,如
encode("hc|m3|te","C:\Users\user\Desktop\\assgn2part1.txt")
我已經非常努力地嘗試,但無法爲此獲得解決方案。請幫我
爲什麼你不能只讓3個參數,前兩個被字符串傳遞給maketrans? – Josiah