1. 程式人生 > >Python文件數據分割

Python文件數據分割

沒有 odi close lse else lines utf 快遞 line

我說:老師你好,你是教音樂的麽?

他說:是的。教音樂修養的

我說:老師,你們的音樂課太無聊了,怎麽不多欣賞現代流行音樂呢,非得枯燥的古典樂器?

他說:你安心學吧!

==============================================================================================

她說:快遞號?

我說:xxxx

她說:沒有!

我說:咋會啊?明明給我發了短信的。

她說:明明發給你的,你去找明明要,別擋著,下一位!5,與以為同學對話

我說:同學,能和你聊聊麽?

===============================================================================================

我說:你咋不吃飯呢?

她說:不餓

我說:哦

她說:你去吃吧

我說:哦

她說:快去

我說:哦!


f = open('D:\\桌面\\record.txt',encoding = 'utf-8')

me = []

he = []

she = []

count = 1


for each_line in f:

if each_line[:6] != '======':

(role, line_spoken) = each_line.split(':',1)

if role == '我說':

me.append(line_spoken)

if role == '他說':

he.append(line_spoken)

if role == '她說':

she.append(line_spoken)

else:

file_name_me = 'Me_' + str(count) + '.txt'

file_name_he = 'He_' + str(count) + '.txt'

file_name_she = 'She_' + str(count) + '.txt'


me_file = open(file_name_me,'w')

he_file = open(file_name_he,'w')

she_file = open(file_name_she,'w')

me_file.writelines(me)

he_file.writelines(he)

she_file.writelines(she)


me_file.close()

he_file.close()

she_file.close()


me = []

he = []

she = []

count += 1


file_name_me = 'Me_' + str(count) + '.txt'

file_name_he = 'He_' + str(count) + '.txt'

file_name_she = 'She_' + str(count) + '.txt'


me_file = open(file_name_me,'w')

he_file = open(file_name_he,'w')

she_file = open(file_name_she,'w')

me_file.writelines(me)

he_file.writelines(he)

she_file.writelines(she)


me_file.close()

he_file.close()

she_file.close()


f.close()















Python文件數據分割