I am having file in which some binary data available for example:
0100010000101111101101001011010000101110111101111110111110111111111000000000001
I want to convert 11 into 01
For example, if it is 01 that will be 01, if 011 it will be 001, if 0111 will be 0001, 01111 will be 00001
So after converting my above data will be:
0100010000100000100101001001010000100010000100000010000010000000001000000000001
I am using find and replace method multiple times.
fin = open("1.txt", "rt")
data = fin.read()
data = data.replace('011', '001')
fin.close()
fin = open("2.txt", "wt")
fin.write(data)
fin.close()
os.remove('1.txt')
fin = open("2.txt", "rt")
data = fin.read()
data = data.replace('011', '001')
fin.close()
fin = open("1.txt", "wt")
fin.write(data)
fin.close()
os.remove('2.txt')
Can some guide me how I can do it in one code using regex or if else statement? I just started learning python.
You can simply replace a
1which is followed by another1with a0:Output: