import os,re,json

def form_decimals(string):
    string = re.sub(r"[\s]+",r"\t",string)
    numerals_list = [n for n in string.split("\t") if len(n)>0]
    for i in range(len(numerals_list)):
        numeral = numerals_list[i]
        str_numeral = "%.3f" %float(numeral)
        numerals_list[i] = str_numeral
    return "\t".join(numerals_list)

with open("文本文件/手动统计结果.txt","r",encoding = "utf8") as f:
    data = f.read()

blocks = re.findall(r"\[BEGIN\]([\s\S]*?)\[END\]",data)

results_dict = {}
for block in blocks:
    temp_list = [l.strip() for l in block.split("\n") if l.strip() != ""]    
    for line in temp_list:
        if line[:2] == "##":
            date = line[2:].strip()
        elif line[:2] == "**":
            current_class = line[2:].strip()
        else:
            separating_pos = re.search("\s",line).span(0)[0]
            if not line[:separating_pos].zfill(6) in results_dict:
                results_dict[line[:separating_pos].zfill(6)] = [date + "\t" + current_class + "\t" + form_decimals(re.sub("\s+?","\t",line[separating_pos:])).strip()]
            else:
                results_dict[line[:separating_pos].zfill(6)].append(date + "\t" + current_class + "\t" + form_decimals(re.sub("\s+?","\t",line[separating_pos:])).strip())

output_data = "usages\n"
for id in results_dict:
    output_data += id + "\n"
    output_data += "\n".join(results_dict[id])
    output_data += "\n\n"

with open("文本文件/metadata.txt","w",encoding = "utf8") as f:
    f.write(output_data)
