import os, sys import csv import struct from collections import defaultdict, Counter def process(data_path, ofilename): ofile = open(ofilename, "w") ofile_core = open(ofilename+".core", "w") writer = csv.writer(ofile) writer_core = csv.writer(ofile_core) for f in sorted(os.listdir(data_path)): if not f.endswith("sort"): continue print(f) ifile = open("{}/{}".format(data_path, f)) reader = csv.reader(ifile) for req in reader: if req[4] == "0" or req[4] == "-1" or req[4] == "": continue # if req[9] == "": # continue req_new = [req[0], req[1], req[2], req[4], req[7], req[9], req[10], req[11]] req_core = [req[0], req[1], req[2], req[4], req[7], ] # print(req) # print([len(req), req[9], req[10], req[11], req[12], req[13]]) # break writer.writerow(req_new) writer_core.writerow(req_core) def convert(ifilename, ofilename): ifile = open(ifilename, "r") reader = csv.reader(ifile) ofile = open(ofilename, "wb") s = struct.Struct(" 2: cat_cnt[req[2].split("/")[1]] += 1 else: cat_cnt[1] += 1 if req[0] in country_map: country = country_map.get(req[0]) else: country = len(country_map) + 1 country_map[req[0]] = country if req[1] in server_map: server = server_map.get(req[1]) else: server = len(server_map) + 1 server_map[req[1]] = server # no cat cat = 1 if len(req[2].split("/")) > 2: if req[2].split("/")[1] in cat_map: cat = cat_map.get(req[2].split("/")[1]) else: cat = len(cat_map) + 2 cat_map[req[2].split("/")[1]] = cat obj_id = hash(req[2]) & 0xffffffff size = int(req[3]) ofile.write(s.pack(obj_id, size, country, server, cat)) ofile.close() for map_data, name in zip((country_map, server_map, cat_map), ("country_map", "server_map", "cat_map")): with open(name, "w") as ofile: for k, v in map_data.items(): ofile.write("{}: {}\n".format(k, v)) print(country_cnt) print(server_cnt) print(cat_cnt) if __name__ == "__main__": # process("/disk2/twr/cdn/", "/disk2/twr/cdn.csv") convert("/disk2/twr/cdn.csv.core", "/disk2/twr/cdn.bin")