from bs4 import BeautifulSoup if __name__ == "__main__": f = open("./amitaro.htm", "r") txt = f.read() soup = BeautifulSoup(txt) import json f = open('amitaro.json') file_list = json.load(f) td = soup.find_all('td') for i, val in enumerate(td): if len(val.contents) == 0: continue key = val.contents[0] if key in file_list: #print(td[i-1].contents[0]) if len(td[i-1].contents) > 0: #print(td[i-1].contents[0]) temp = BeautifulSoup(str(td[i-1].contents[0])) a = temp.find_all('a') print(a[0].contents[0]) file_list[key]["kana"] = str(a[0].contents[0]) for key, val in file_list.items(): val["path"] = "./amitaro/" + val["path"] with open("./amitaro_with_kana.json", "w") as outfile: outfile.write(json.dumps(file_list, indent=4,ensure_ascii=False)) file = [] for key, val in file_list.items(): if len(val['kana']) == 0: continue if val['kana'].find("(") != -1: continue file.append(f"{val['path']}|0|{val['kana']}") amitaro_train = [] amitaro_val = [] for val in file: amitaro_train.append(val) f = open("amitaro_train.txt", "w") for val in amitaro_train: f.write(f"{val}\n") f.close()