上一篇文章中提到對solidity源代碼的註釋和空格的處理;有時候做數據處理時只需要一些指定的代碼行,一些對實驗無用的代碼應該去掉,這樣有助於我們隊數據進行分析。在我們所做的實驗中,我需要提取的是和call.value這個關鍵字相關的代碼段,這裏我們先進行了一些初步的處理,提取call.value的前10行代碼和後10行代碼。
(1)定位文件中call.value的位置
def find_location(filepath):
f = open(filepath, 'r')
lines = f.readlines()
f.close()
line_len = len(lines)
location = 0
for i in range(line_len):
if 'call.value' in lines[i]:
location = i
extract_selected_code(filepath, location)
對於一個文件,首先我們需要定位到call.value的位置,找到該位置之後,我們就可以提取該位置的前10行和後10行的代碼了。
(2)提取指定的代碼段
def extract_selected_code(filepath, location):
f = open(filepath, 'r')
lines = f.readlines()
f.close()
line_len = len(lines)
result = None
result1 = None
result2 = None
if location - 10 >= 0:
result1 = lines[location - 10:location]
print('前10行:', result1)
else:
result1 = lines[0:location]
print('前10行:', result1)
if location + 10 <= line_len:
result2 = lines[location:location + 10]
print('後10行:', result2)
else:
result2 = lines[location:line_len]
print('後10行:', result2)
result = result1 + result2
print(result)
newFilePath = '../train_data_V2/'
writeResult(newFilePath + filepath.split('/')[2], result)
在這裏我們進行了一些小的處理,如果call.value的關鍵字在最前面或最後面,那麼直接運行會獲取不到代碼段;由此,我們在這裏加入了基本的判斷,見(2)。