Python檔案資料分割

來源:互聯網
上載者:User

標籤:ken   odi   file   else   xxx   簡訊   老師   role   +=   

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

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

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

他說:你安心學吧!

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

她說:快遞號?

我說: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檔案資料分割

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.