import os METADAT = { } CSV = "file_name,country,region,gender,age,date,text" for root, folders, files in os.walk(os.path.dirname(__file__)): if files: r = root.split(os.path.dirname(__file__) + "/")[-1] country = r.split("/")[0] wavs = [f for f in files if f.endswith(".wav")] txt = [f for f in files if f.endswith(".txt")] for idx, t in enumerate(txt): wav = f"{r}/" + t.replace(".txt", ".wav") #print(wav) METADAT[wav] = {"country": country} #print(t) with open(f"{root}/{t}") as f: lines = f.read().split("\n") keys = [l for l in lines if ":" in l] txt[idx] = ". ".join([l.lstrip("-> ") for l in lines if l.startswith("-")]).replace('"',"'") for k in keys: k, v = k.split(":", 1) k = k.lower().strip() v = v.strip() if v: METADAT[wav][k] = v CSV += f"\n{wav},{country},{METADAT[wav].get('local', '').split('-',1)[-1].replace(',', ' -').strip()},{METADAT[wav].get('sexo', '')},{METADAT[wav].get('idade', '').split('anos')[0].strip()},{METADAT[wav].get('data', '')},\"{txt[idx]}\"" #print(txt) with open(f"{os.path.dirname(__file__)}/metadata.csv", "w") as f: f.write(CSV)