s.matthew.english s.matthew.english - 28 days ago 6
Python Question

Eliminate all text before specific character in output loop using regex in Python

I have data that looks like this:

['6005600401']
['000000: PUSH1 0x05']
['000002: PUSH1 0x04']
['000004: ADD']


The initial representation it's derived from is here:

6005600401
000000: PUSH1 0x05
000002: PUSH1 0x04
000004: ADD


The output I'd like to create is like so:

PUSH1 0x60, PUSH1 0x40, MSTORE, CALLDATASIZE, ISZERO, PUSH2 0x006c, JUMPI, PUSH1 0xe0, PUSH1 0x02, EXP, PUSH1 0x00, CALLDATALOAD, DIV


I've been experimenting with different techniques of isolating that data in the python shell like so:

>>> date_div = "Blah blah blah, Updated: Aug. 23, 2012"
>>> date_div.split('Updated: ')
['Blah blah blah, ', 'Aug. 23, 2012']
>>> date_div.split('Updated: ')[-1]
'Aug. 23, 2012'
>>> line = ['000000: PUSH1 0x05']
>>> date_div.split(':')
['Blah blah blah, Updated', ' Aug. 23, 2012']
>>> line.split(':')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'split'
>>> line = ["000000: PUSH1 0x05"]
>>> line.split(':')


But as of yet I've not been able to eliminate all the superfluous characters. How can I employ regex such that my data goes from a representation like this:

['6005600401']
['000000: PUSH1 0x05']
['000002: PUSH1 0x04']
['000004: ADD']


Yo one like this:

PUSH1 0x60, PUSH1 0x40, MSTORE, CALLDATASIZE, ISZERO, PUSH2 0x006c, JUMPI, PUSH1 0xe0, PUSH1 0x02, EXP, PUSH1 0x00, CALLDATALOAD, DIV





Here is the script used to create it:

import csv
import sys
import subprocess

def my_test_func(filename, data):
with open(filename, 'w') as fd:
fd.write(data)
fd.write('\n')
return subprocess.check_output(['evm', 'disasm', filename])

if '__main__' == __name__:

file_name = sys.argv[1]
byte_code = sys.argv[2]
status = my_test_func(file_name, byte_code)

edits = csv.reader(status.splitlines(), delimiter=",")
for row in edits:
print(row)

Answer Source

Why not just split on ':'?

for line in status:
    data = line.split(:)
    if len(data) < 2:
       continue
    print(data[1].strip())