[英]Eliminate all text before specific character in output loop using regex in Python
我有看起來像這樣的數據:
['6005600401']
['000000: PUSH1 0x05']
['000002: PUSH1 0x04']
['000004: ADD']
它的原始表示形式如下:
6005600401
000000: PUSH1 0x05
000002: PUSH1 0x04
000004: ADD
我要創建的輸出如下所示:
PUSH1 0x60, PUSH1 0x40, MSTORE, CALLDATASIZE, ISZERO, PUSH2 0x006c, JUMPI, PUSH1 0xe0, PUSH1 0x02, EXP, PUSH1 0x00, CALLDATALOAD, DIV
我一直在嘗試不同的技術來隔離python shell中的數據,如下所示:
>>> 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(':')
但是到目前為止,我還不能消除所有多余的字符。 如何使用正則表達式,以便我的數據來自這樣的表示形式:
['6005600401']
['000000: PUSH1 0x05']
['000002: PUSH1 0x04']
['000004: ADD']
喲這樣的:
PUSH1 0x60, PUSH1 0x40, MSTORE, CALLDATASIZE, ISZERO, PUSH2 0x006c, JUMPI, PUSH1 0xe0, PUSH1 0x02, EXP, PUSH1 0x00, CALLDATALOAD, DIV
這是用於創建它的腳本:
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)
為什么不只在':'上分開?
for line in status:
data = line.split(:)
if len(data) < 2:
continue
print(data[1].strip())
這是一種好方法:
opcodes_list = list()
for element in status.split('\n'):
result = re.search(r"\b[A-Z].+", element)
if result:
opcodes_list.append(result.group(0))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.