NLP:利用python编程语言的split函数结合if判断(T1自定义函数或T2封装函数)实现提取两人对话内容(***分隔txt文档),并各自保存为txt文档

NLP:利用python编程语言的split函数结合if判断(T1自定义函数或T2封装函数)实现提取两人对话内容(***分隔txt文档),并各自保存为txt文档

目录

问题探究

实现代码


问题探究

NLP:利用python编程语言的split函数结合if判断(T1自定义函数或T2封装函数)实现提取两人对话内容(***分隔txt文档),并各自保存为txt文档_第1张图片

实现代码

f=open("niu.txt")
interviewer=[]
Qtum=[]
count=1
for each_line in f:
    if each_line[:6]!="******":
        (role,line_spoken)=each_line.split(":",1)      #字符串分割操作,默认以空格切片,1是取右边
        if role =="提问":
            interviewer.append(line_spoken)
        if role =="帅初":
            Qtum.append(line_spoken)
    else: #文件的分别操作
        file_name_interviewer="interviewer_"+str(count)+".txt"
        file_name_Qtum="Qtum_"+str(count)+".txt"
        interviewer_file=open(file_name_interviewer,"w")
        Qtum_file=open(file_name_Qtum,"w")
        
        interviewer_file.writelines(interviewer)
        Qtum_file.writelines(Qtum)
        
        interviewer_file.close()
        Qtum_file.close()
        
        interviewer=[]
        Qtum=[]
        count += 1
f.close()


相关文章

https://www.cnblogs.com/yunyaniu/articles/8540855.html

你可能感兴趣的:(Python编程(初级+进阶),NLP,NLP)