-
Notifications
You must be signed in to change notification settings - Fork 2
/
behavior_extract.py
42 lines (36 loc) · 1.27 KB
/
behavior_extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import csv
import collections
# this class is used to clean the raw .csv file and generate training/testing dataset
class create_pe_imports_set:
feature_names = set()
names_values = []
def __init__(self):
# open file 1
try:
file = open('new_output_2/behavior.csv', 'r')
csv_reader_lines = csv.reader(file)
except IOError:
print('Cannot open file')
data = []
for row in csv_reader_lines:
data.append(row)
for i in range(0, len(data)):
count = []
for j in range(len(data[i])):
count.append(data[i][j])
self.feature_names.add(data[i][j])
dic = collections.Counter(count)
self.names_values.append(dic)
def outputCsv(self):
csvFile = open("new_behavior_analysis_2.csv", 'w', newline='')
try:
writer = csv.DictWriter(csvFile, fieldnames=list(self.feature_names), restval=0)
writer.writeheader()
for dic in self.names_values:
writer.writerow(dic)
print('The clean data file has been saved')
finally:
csvFile.close()
if __name__ == '__main__':
dynamic_set = create_pe_imports_set()
dynamic_set.outputCsv()