Hi all,

I have a data set that has a text header, followed by binary data. I want to search for a certain variable in the text header, store whatever name comes after the variable, and then replace it with X. Since I want to leave the binary alone, I have used a try/except statement to note when the binary has been reached, and to not do anything to it. This is what I have come up with.

import sys

fpath = sys.argv[1]

print (fpath)

replace = []

f = open(fpath, 'r+')
tell = f.tell()
line = f.readline()

while(True):
    if ("<ParamString.\"Name\">" in line):
        if ("}" in line):
            strlen = len(line.split("\"") )[1]  
            xstr = "\""
            for i in range(0,strlen):
                xstr = xstr+'x'
            xstr += "\""
            repline = line.split("{")[0] + xstr + line.split("}")[1]
            replace.append(tell)
            replace.append(line)
           
        else:
            line = f.readline()
            repline = (f.write)
            replace.append(tell)
            replace.append(line)

    tell = f.tell()
    try:
            line = f.readline()
    except UnicodeDecodeError:
                     print ("have reached binary code")
                     break

f.close()


f = open(fpath, 'r+')
for tell in replace:
    f.seek(tell)
    f.write(line)
f.close()

However, what results is this error:

Traceback (most recent call last):
File "code3.py", line 43, in <module>
f.seek(tell)
TypeError: unorderable types: str() < int()

I think there is a problem with using "append" for tell and line separately, but I can't do replace.append(tell,line) because it doesn't work with storing two coordinates at once.

Any help/guidance greatly appreciated!

I would suggest printing the first few items in replace. I think you will find that there is more than "tell" in replace, so the line
for tell in replace:
yields an error. You might try inserting this modification:

replace.append(tell)
replace.append(line)
#
# change to
replace.append((tell, line))
#
# and then -----------------------
for tell in replace:
#
# becomes
for item in replace:
    for tell, line in item:
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.