regex - chunk of data into fixed lengths chunks and then add a space and again add them all as a string -
i have got hex values a85b080040010000. want a8 5b 08 00 40 01 00 00. have done using below code. have work large data. want computed time low.
import binascii import re filename = 'calc.exe' open(filename, 'rb') f: content = f.readline() text = binascii.hexlify(content) text1 = binascii.unhexlify(text) length1 = 32 length2 = 16 list = re.findall('.{%d}' % length1, text) list1 = re.findall('.{%d}' % length2, text1) d = [] in range (0, len(list), 1): temp = "" l = re.findall('.{%d}' % length2, list[i]) s = l[0] t = iter(s) temp += str(' '.join(a+b a,b in zip(t, t))) temp += " " s = l[1] t = iter(s) temp += str(' '.join(a+b a,b in zip(t, t))) temp += " | " + list1[i] print temp
you can
x="a85b080040010000" print re.sub(r"(.{2})",r"\1 ",x) or
x="a85b080040010000" print " ".join([i in re.split(r"(.{2})",x) if i])
Comments
Post a Comment