0

So, I'm trying to create a program that will automatically edit a specific set of characters in a file (it will read and replace them). No other data can be moved in the file otherwise it might become corrupted so I need to replace the text in the exact same place as before. I have looked around and found nothing useful but here is my code so far:

l = 3
w = 0
with open("InidCrd000.crd") as myfile:
    hexWord = myfile.readlines()[l].split()[w]
    codeA = hexWord[58]
    codeB = hexWord[59]
    print("Current value: ", codeA, codeB)
    codeA = " "
    codeB = "Ð"
    print("New value: ", codeA, codeB)

EDIT - I now have this code (credit - Ilayaraja), which works but then it breaks the file up into lines and places random data in incorrect positions (although the inputted data is in the correct position):

def replace(filepath, lineno, position, newchar):
    with open(filepath, "r") as reader:
        lines = reader.readlines()
        l = lines[lineno-1]
        l = l[0:position] + newchar + l[position+1:]
        lines[lineno-1] = l
    with open(filepath, "w") as writer:
        writer.writelines(lines)
replace("InidCrd000.crd", 4, 57, "")
replace("InidCrd000.crd", 4, 58, "Ð")

If you want the file for testing, here it is: 1drv.ms/u/s!AqRsP9xMA0g1iqMl-ZQbXUqX2WY8aA (It's a onedrive file)

2 Answers2

0

first find the code you want to change using this :

l = 3
w = 0
with open("InidCrd000.crd") as myfile:
    hexWord = myfile.readlines()[l].split()[w]
    codeA = hexWord[58]
    codeB = hexWord[59]
myfile.close()

then change like this :

import fileinput

with fileinput.FileInput(fileToSearch, inplace=True, backup='.bak') as file:
    for line in file:
         line.replace(codeA, textToReplace)
Vikas Periyadath
  • 3,088
  • 1
  • 21
  • 33
0

Define a function with the arguments the path of the file(filepath), line number(lineno 1 to N), position of the character in the line(position 0 to N) and the new character to be overwritten(newchar) as follows:

def replace(filepath, lineno, position, newchar):
    with open(filepath, "r") as reader:
        lines = reader.readlines()
        l = lines[lineno-1]
        l = l[0:position] + newchar + l[position+1:]
        lines[lineno-1] = l
    with open(filepath, "w") as writer:
        writer.writelines(lines)

You can call the function as follows to replace the characters:

replace("InidCrd000.crd", 3, 58, " ")
replace("InidCrd000.crd", 3, 59, "Ð")
Ilayaraja
  • 2,388
  • 1
  • 9
  • 9
  • The code worked but it has moved all the characters, so now the replaced text has moved from CD-CE to C8-C9. This is because python has broken up the file into different segments from what it originally was. – Ricochet1136 Jan 17 '18 at 10:34
  • Here's the stock file: https://1drv.ms/u/s!AqRsP9xMA0g1iqMl-ZQbXUqX2WY8aA (It's a onedrive file) – Ricochet1136 Jan 17 '18 at 10:44
  • @Ricochet1136 It is not accessible. – Ilayaraja Jan 17 '18 at 12:45
  • Not defaultly because that's how it's supposed to be but it can be viewed in notepad (HxD can also be used to view the hex) – Ricochet1136 Jan 17 '18 at 12:49