码迷,mamicode.com
首页 > 编程语言 > 详细

python3.3:文本文件读取、处理及保存。小练习

时间:2015-06-21 00:41:22      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:

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+) #以读写方式处理文件IO
fo.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.close


fo2 = open(d:/test111.txt,"w")
fo2.write(html)
fo2.close()

 

python3.3:文本文件读取、处理及保存。小练习

标签:

原文地址:http://www.cnblogs.com/qq21270/p/4591318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!