python实现将一个记事本中的一行文本拆分开写入另一个记事本
问题描述
实现将下图的文本, 按照此格式写入:
代码实现
'''
@Filename :try.py
@Description :
@Datatime :2021/12/13 21:22:42
@Author :qtxu
@Version :v1.0
@Function : 实现将按行存放sentence label的一行,一个词+对应label 一行写入,一句话写完,空行
'''
file1 = "/home/qtxu/semeval_data/4.txt"
file2 = "/home/qtxu/semeval_data/5.txt"
f = open(file1,"r",encoding='utf-8')
f2 = open(file2,"w")
lines = f.readlines()
j = 0
for line in lines:
sentence = line.strip().split("\t")[0]
labels = line.strip().split("\t")[1]
sentence_arr = sentence.strip().split(" ")
labels_arr = labels.strip().split(" ")
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()
|