python3.3:文字檔讀取、處理及儲存。小練習

來源:互聯網
上載者:User

標籤:

1、編輯工具:PyCharm4.5.1

http://www.jetbrains.com/pycharm/download/   下載左側的完全版

2、安裝好python3.3.5後

http://rj.baidu.com/soft/detail/17016.html

3、讀取chengyu.txt,按行處理後,儲存到test111.txt中。文字檔都是utf-8的

 

# -*- coding: utf-8 -*-html = ""fo = open(‘d:/chengyu.txt‘, ‘a+‘) #以讀寫方式處理檔案IOfo.seek(0)str_line = fo.readline()while str_line:    # print(str_line)    str_line = fo.readline()    # 有兩種標籤<h3> 和 <p>,處理<p>標籤中所含的成語    # str_line = "<p>急中生智 仰不愧天 鎮定自若 化險為夷</p>"    str_line2 = ""    is_word = str_line.find("<p>")    if is_word>-1:        print("----是成語所在行----")        # print(str_line)        str_line = str_line.replace("<p>","")        str_line = str_line.replace("</p>","")        str_line = str_line.replace("\n","")        for _word in str_line:            if _word.find(" ")>-1:                str_line2 = str_line2 + "</u>" + _word + "<u>"            elif _word.find("、")>-1:                str_line2 = str_line2 + "</u>" + _word + "<u>"            else:                str_line2 = str_line2 + _word        str_line2 = "<u>" + str_line2 + "</u>"        html = html + "<p>" + str_line2 + "</p>\n"    else:        print("----NO-所在行----")        html = html + str_line        # print(str_line)fo.closefo2 = open(‘d:/test111.txt‘,"w")fo2.write(html)fo2.close()

 

python3.3:文字檔讀取、處理及儲存。小練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.