python3.4.3下逐行读入txt文本并去重的方法-创新互联

读写文件时应注意的问题包括:

成都创新互联专注于甘孜州企业网站建设,成都响应式网站建设,商城开发。甘孜州网站建设公司,为甘孜州等地区提供建站服务。全流程按需制作,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务

1.字符编码

2.操作完成即时关闭文件描述符

3.代码兼容性

几种方法:

#!/bin/python3
original_list1=[" "]
original_list2=[" "]
original_list3=[" "]
original_list4=[" "]
newlist1=[" "]
newlist2=[" "]
newlist3=[" "]
newlist4=[" "]
newtxt1=""
newtxt2=""
newtxt3=""
newtxt4=""
#first way to readline
f = open("duplicate_txt.txt","r+")    # 返回一个文件对象  
line = f.readline()           # 调用文件的 readline()方法 
while line:  
  original_list1.append(line)          
  line = f.readline()  
f.close() 
#use "set()" remove duplicate str in the list
# in this way,list will sort randomly
newlist1 = list(set(original_list1))
#newlist1 = {}.fromkeys(original_list1).keys() #faster 
#rebuild a new txt 
newtxt1="".join(newlist1)
f1 = open("noduplicate1.txt","w")
f1.write(newtxt1)
f1.close()
###################################################################
#second way to readline
for line in open("duplicate_txt.txt","r+"):  
  original_list2.append(line)
newlist2 = list(set(original_list2))
newlist2.sort(key=original_list2.index)         #sort
#newlist2 = sorted(set(original_list2),key=l1.index)  #other way
newtxt2="".join(newlist2)
f2 = open("noduplicate2.txt","w")
f2.write(newtxt2)
f2.close()
###################################################################
#third way to readline
f3 = open("duplicate_txt.txt","r")  
original_list3 = f3.readlines()       #读取全部内容 ,并以列表方式返回 
for i in original_list3:          #遍历去重
  if not i in newlist3:
      newlist3.append(i)
newtxt3="".join(newlist3)
f4 = open("noduplicate3.txt","w")
f4.write(newtxt3)
f4.close()
###################################################################
#fourth way
f5 = open('duplicate_txt.txt',"r+") 
try: 
  original_list4 = f5.readlines() 
  [newlist4.append(i) for i in original_list4 if not i in newlist4]
  newtxt4="".join(newlist4)
  f6 = open("noduplicate4.txt","w")
  f6.write(newtxt4)
  f6.close()
finally: 
  f5.close()

本文题目:python3.4.3下逐行读入txt文本并去重的方法-创新互联
文章URL:http://abwzjs.com/article/dhigjg.html