玖叶教程网

前端编程开发入门

Python入门:文件内容去重操作

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  学习是一种态度,只要你有态度,学习将会是一种乐趣+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#Author:FrankHacker#python3.5''' 1、对特定文件内容进行去重操作 2、请输入需要去重文件的绝对路径 3、删除文件中的空行 4、去除字符串前后空行 5、生成一个以当前日期命名的文件 '''importtime 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]:")defopenThefile():''' 1、打开要去重的文件 2、删除每行数据前后的无用字符 :return: '''ff = open(f,'r') l = []#for i in ff.readline(): #readline是文件中的第一行内容foriinff.readlines():#所有内容中的每一行ii = i.replace('\t','').strip() l.append(ii) ff.close()returnldefcreateNewfile(openThefile):''' 去重操作 :param openThefile: :return: '''l = []foriinopenThefile:ifinotinl: l.append(i)''' 创建新文件 '''filename = f[:f.find('.')] postfix = f[f.find('.'):] theNewfile = open(filename + nowtime + postfix,'a')''' 写入处理过的内容 '''foriinl: theNewfile.writelines(i+'\n') theNewfile.close()if__name__ =='__main__': createNewfile(openThefile())

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

学习Python的同学注意了!!!学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Python学习交流群,群号码:253470243我们一起学Python!

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言