import os
import csv

CATA = 'Trojan-Downloader'
CSV_DIR_1 = 'G:/API_result/apiseq_'+ CATA +'.csv'
CSV_DIR_2 = 'G:/API_result/apiseq_malware.csv'
csv_reader_1 = csv.reader(open(CSV_DIR_1))
csv_reader_2 = csv.reader(open(CSV_DIR_2))

md5_dic = {}

print(type(csv_reader_1))

csv_1 = []
for row in csv_reader_1:
    md5 = row[0]
    # print('md5',md5)
    md5_dic[md5] = True
    csv_1.append(row)

# print(csv_1)

# 去重部分
pku_name_2_md5_dic = {}
fopen = open('pku_name_2_md5.json','r')
pku_name_2_md5_dic = eval(fopen.read())
fopen.close()

tot = 0
for row in csv_reader_2:
    name = row[0]
    
    label = name.split('.')[0]
    if not label == CATA:
        continue
    print('name', name)
    tot = tot + 1
    if not name in pku_name_2_md5_dic:
        print('不在dict中，跳过')
        continue
    md5 = pku_name_2_md5_dic[name]['md5']
    if md5 in md5_dic:
        print('重复去除')
        continue
    new_row = row
    new_row[0] = md5
    csv_1.append(new_row)

print('共有', tot, '个'+ CATA)

csvFile = open('G:/API_result_union/apiseq_'+CATA+ '.csv', 'w',encoding='utf8',newline='')

writer = csv.writer(csvFile)
for i in range(len(csv_1)):
    writer.writerow(csv_1[i])
csvFile.close()








