实现将txt中,原本按照sentence label存放的文本,如下图所示:
存放成按照每个单词及其对应label 为一行,一句话之后空一行的形式存放,如下图:
def sentence_to_two(file1,file2): # file是对应图一的地址,file2是对应图2的存放地址 f = open(file1,"r",encoding='utf-8') f2 = open(file2,"w") lines = f.readlines() # print(lines) j = 0 for line in lines: sentence = line.strip().split("t")[0] # print(sentence) labels = line.strip().split("t")[1] # print(label) sentence_arr = sentence.strip().split(" ") labels_arr = labels.strip().split(" ") # print(arr) i = 0 for word in sentence_arr: f2.write(sentence_arr[i]+" "+labels_arr[i]+"n") i = i+1 j = j+1 f2.write("n") f.close() f2.close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)