1

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)
4
  • I would probably use some sort of rolling range, with a variable start and a variable end. Keep adding two to each as you iterate through the string in a for loop. Commented Aug 6, 2021 at 12:14
  • @DanCurry , Thanks for your reply 🌻. May I ask you to give me the code snippet? Commented Aug 6, 2021 at 12:16
  • If I run your code I do get 'bdad'. Commented Aug 6, 2021 at 12:17
  • @joostblack , because the text file is too long not just a "01110011". probably this a special case 🤔 Commented Aug 6, 2021 at 12:20

4 Answers 4

6

Just build a new string only substituting the 2-char substrings at even indeces:

repl = {
    '00': 'a',
    '01': 'b',
    '10': 'c',
    '11': 'd',
}


filedata = ''.join(repl[filedata[i:i+2]] for i in range(0, len(filedata), 2))
Sign up to request clarification or add additional context in comments.

Comments

0

Omitting the file handling, you could do this:-

mystring = '01110011'
mymap = {'00': 'a', '01': 'b', '10': 'c', '11': 'd'}
newstring = ''
while len(mystring) >= 2:
    newstring += mymap[mystring[:2]]
    mystring = mystring[2:]
print(newstring)

4 Comments

This is much less efficient than @schwobaseggl's answer.
No it isn't. I timed it. List comprehensions look compact but are not necessarily more efficient than writing your own loop. If you want to contact me by chat I'd be more than happy to show you my proof
Are you sure? Usually string operations like newstring += mymap[mystring[:2]] and mystring = mystring[2:] cause memory to be reallocated.
As I said, I'm more than happy to show you my proof. Posting code in comments is frowned upon and my proof isn't a 'solution' so, as I said, we can do this over chat
0

This would help:

with open('file.txt', 'r') as file :
  filedata = file.read()

New_String = ""
for i in range(0, len(filedata), 2):
    if filedata[i:i+2] == "00" : New_String += "a"
    if filedata[i:i+2] == "01" : New_String += "b"
    if filedata[i:i+2] == "10" : New_String += "c"
    if filedata[i:i+2] == "11" : New_String += "d"

print(New_String)

Comments

0

Here's a NumPy solution.

import numpy as np

# Read in the file
with open('file.txt', 'r') as file:
    byte_array = np.fromfile(file, dtype='uint8').reshape(-1, 2)

result = np.full((byte_array.shape[0], 1), b' ')
result[np.all(byte_array == (48, 48), axis=1)] = b'a'
result[np.all(byte_array == (48, 49), axis=1)] = b'b'
result[np.all(byte_array == (49, 48), axis=1)] = b'c'
result[np.all(byte_array == (49, 49), axis=1)] = b'd'

# Write the file out again
with open('file.txt', 'w') as file:
    file.write(result.tobytes().decode('utf-8'))

For more details on this method, see this answer.

Or, more conveniently, you could replace the middle section with:

repl = {
    (48, 48): b'a',
    (48, 49): b'b',
    (49, 48): b'c',
    (49, 49): b'd',
}
result = np.full((byte_array.shape[0], 1), b' ')
for k, v in repl.items():
    result[np.all(byte_array == k, axis=1)] = v

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.