I have a text file (*.txt) which its content is "01110011"
and I want to replace that such that: '00' ==> a
, '01' ==> b
, '10' ==> c
, '11' ==> d
from left to right. so the content becomes 'bdad'
.
According to this post, I used the code below but unfortunately, the replacement isn't directional (I mean it's not from left to right).
May I ask you to help me, please?
# Read in the file
with open('file.txt', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('00', 'a')
filedata = filedata.replace('01', 'b')
filedata = filedata.replace('10', 'c')
filedata = filedata.replace('11', 'd')
# Write the file out again
with open('file.txt', 'w') as file:
file.write(filedata)