Issue
I need to convert the input into desired output by using python.
The rule is to match all the indices with the numbers of the each second row. and if the size is larger or equal to 10, then the number needs 1 extra space per 10 numbers. (ex. if the size is 025, 2 spaces for 2 times) FYI, the numbers inside '[]' are hexadecimal format.
The input and desired output are shown as below:
<input.txt>
[00], idx=06, size=001
[06000] 00
[01], idx=07, size=001
[07000] 00
[02], idx=20, size=007
[20000] 00 00 00 00 40 00 00
[03], idx=33, size=002
[33000] 2b 03
[04], idx=45, size=015
[45000] ff 07 00 00 00 00 00 00 00 00
[4500a] 00 00 00 00 00 00 00 00 00 00
[45014] 00 00 00 00 00
<output.txt>
[index]06 00
[index]07 00
[index]20 00 00 00 00 40 00 00
[index]33 2b 03
[index]45 ff 07 00 00 00 00 00 00 00 00 00 00 00 00
In order to achieve the goal, There are the steps I came up with.
- put [index] on beginning of every line.
- list all the numbers that matches with 'idx'
- extend the numbers of the second rows of index.
and I've managed to do the following so far:
import sys,os,re
def get_index(f):
for line in f.readlines():
if "idx" in line:
yield line[10:12]
def main():
with open("input.txt", "r") as f:
with open("output.txt", "w") as oup:
for line in get_index(f):
oup.write('[index]' + line + '\n')
if __name__ == '__main__':
main()
for me, step 3 seems to be hard.. How can I develop it from here? Or any better idea to solve this? Any help will be appreciated. Thank you very much.
Solution
I think this is a 2-step process.
Consume the file building a dictionary of the data you're interested in.
Use re to isolate the idx and size values rather than fixed offsets.
Once you have the data in a manageable structure, you can then work through the dictionary to generate the required output.
Here's one way of doing it:
import re
INFILE = 'input.txt'
OUTFILE = 'output.txt'
result = dict()
key = None
with open(INFILE) as txt:
for line in map(str.strip, txt):
if (found := re.findall(r'idx=(\d+).*size=(\d+).*', line)):
key, size = found[0]
result[key] = [int(size)]
else:
result[key].extend(line.split()[1:])
output = []
for k, v in result.items():
output.append(f'[index]{k}')
size, hexvals = v[0], v[1:]
for i in range(0, size, 10):
output[-1] += ' ' if i == 0 else ' '
output[-1] += ' '.join(hexvals[i:min(i+10, size)])
with open(OUTFILE, 'w') as outfile:
print(*output, sep='\n', file=outfile)
Output (in file):
[index]06 00
[index]07 00
[index]20 00 00 00 00 40 00 00
[index]33 2b 03
[index]45 ff 07 00 00 00 00 00 00 00 90 50 00 00 00 60
Answered By - OldBill
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.