Python switching the positions of two particular integers in a text file?

I’d want to swap two numbers on each line of the text file after replacing the equal sign with this ‘|’ and saving it, but I’m having problems doing so! As an example,

123456789=123456789

I envisioned it like follows:

123453489=126756789

As you can see, I switched 34 for 67!
This is my code:

with open('track.txt', 'r') as z:
 data = z.read()
 
data = data.replace('=', '|')
  
with open('track2.txt', 'w') as z:
  z.write(data)

Can I do the same thing as suggested in this doc by using slices and concatenation?
Thank you kindly! Please excuse my English.

1 Like

You could do something like this:

with open('track.txt', 'r') as z:
 data = z.read()
 
data = data.replace('=', '|')
data = data.split('\n')
  
for i in range(len(data)):
 data[i] = data[i].split('|')
 temp = data[i][0]
 data[i][0] = data[i][1]
 data[i][1] = temp
 data[i] = '|'.join(data[i])
 
data = '\n'.join(data)
  
with open('track2.txt', 'w') as z:
 z.write(data)