Python入门:文件内容去重操作
2016-11-07 15:50
232 查看
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # 学习是一种态度,只要你有态度,学习将会是一种乐趣 # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # python3.5 ''' 1、对特定文件内容进行去重操作 2、请输入需要去重文件的绝对路径 3、删除文件中的空行 4、去除字符串前后空行 5、生成一个以当前日期命名的文件 ''' import time year = time.localtime().tm_year mon = time.localtime().tm_mon day = time.localtime().tm_mday hour = time.localtime().tm_hour min = time.localtime().tm_min sec = time.localtime().tm_sec nowtime = str(year) + str(mon) + str(day) + str(hour) + str(min) + str(sec) f = input("please entry the file[absolute path]:") def openThefile(): ''' 1、打开要去重的文件 2、删除每行数据前后的无用字符 :return: ''' ff = open(f, 'r') l = [] # for i in ff.readline(): #readline是文件中的第一行内容 for i in ff.readlines(): # 所有内容中的每一行 ii = i.replace('\t', '').strip() l.append(ii) ff.close() return l def createNewfile(openThefile): ''' 去重操作 :param openThefile: :return: ''' l = [] for i in openThefile: if i not in l: l.append(i) ''' 创建新文件 ''' filename = f[:f.find('.')] postfix = f[f.find('.'):] theNewfile = open(filename + nowtime + postfix, 'a') ''' 写入处理过的内容 ''' for i in l: theNewfile.writelines(i + '\n') theNewfile.close() if __name__ == '__main__': createNewfile(openThefile()) == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == ==
相关文章推荐
- Python入门:文件内容去重操作
- Python 3.6 读取并操作文件内容
- Python入门(六)——self,类,方法,__init__,类对象,类变量,文件操作,判断回文,pickle模块用法
- 快速入门Python中文件读写IO是如何来操作外部数据的?
- python操作文件写入内容
- Python 入门笔记 文件操作
- Python文件操作,open读写文件,追加文本内容(转)
- Python 3从入门到精通13-读文件内容
- Python文件基础操作(入门1)
- python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
- Python文件基础操作(入门1)
- python入门笔记(Day8)--StringIO,BytesIO,环境变量, 操作文件和目录
- 【Python】监控文件内容增删改操作变化
- Python 3从入门到精通12-创建新文件并写入内容,在文件尾部更新内容
- Python文件基础操作(入门1)
- Python入门程序:文件操作和IO异常
- Python文件基础操作(入门1)
- Python入门系列:文件和目录操作
- Python 3.6 读取并操作文件内容的实例
- Python文件操作,with open as追加文本内容实例