虚位以待(AD)
虚位以待(AD)
首页 > 脚本专栏 > python > python3.4.3下逐行读入txt文本并去重的方法

python3.4.3下逐行读入txt文本并去重的方法
类别:python   作者:码皇   来源:互联网   点击:

下面小编就为大家分享一篇python3 4 3下逐行读入txt文本并去重的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

1.字符编码

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

3.代码兼容性

几种方法:

    #!/bin/python3original_list1=[" "]original_list2=[" "]original_list3=[" "]original_list4=[" "]newlist1=[" "]newlist2=[" "]newlist3=[" "]newlist4=[" "]newtxt1=""newtxt2=""newtxt3=""newtxt4=""#first way to readlinef = 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 randomlynewlist1 = 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 readlinefor 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 waynewtxt2="".join(newlist2)f2 = open("noduplicate2.txt","w")f2.write(newtxt2)f2.close()####################################################################third way to readlinef3 = 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 wayf5 = 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()

结果:

去重前:

去重后(无序):

去重后(有序):

总结

这段下程序涉及文件读写操作以及链表List的操作,文章开头提到的几个问题,由于并没有使用中文,所以不关心编码,但这里还是要提一提:

    f = open("test.txt","w")f.write(u"你好")

上面这段代码如果在python2中运行会报错

报错是因为程序没办法直接保存unicode字符串,要经过编码转换成str类型的二进制字节序列才可以保存。

write()方法会自动编码转换,默认使用ascii编码格式,而ascii不能处理中文,所以出现UnicodeEncodeError。

正确方式是在调用write()方法前,手动格式转换,用utf-8或者gbk转换成str。

    f = open("test.txt","w")text=u"你好"text=text.encode(encoding='utf-8')f.write(text)

关于close()问题:

不关闭会有什么影响呢?操作完成后,不关闭文件,会对系统资源造成浪费,因为系统可打开的文件描述符数量是有限的。Linux是65535。

一般来说close之后就OK了,但是也会存在特殊情况,比如说,在调用open()函数时就已经发生错误,权限不足,调用close()肯定报错。还有一种是在write()时,如果磁盘空间不足,报错,close()就没有机会执行了。正确的做法就是使用 try except 对异常进行捕获:

    f = open("test.txt","w")try: text=u"你好" text=text.encode(encoding='utf-8') f.write(text)except: IOError as e: print("oops,%s"%e.args[0])finally: f.close()

更优雅的写法是用 with…as。

    with open("test.txt","w") as f: text=u"你好" f.write(text.encode(encoding='utf-8'))

文件对象实现上下午管理器协议,程序进入with语句时,会把文件对象赋值给变量f,在程序退出with时会自动的调用close()方法。

关于兼容性问题:

python2和python3的open()函数是不一样的,后者可以在函数中指定字符编码格式。

如何解决python2和python3的兼容open()问题呢?

使用io模块下的open()函数,python2中的io.open等价与python3的open函数

    from io import openwith open("test.txt","w",encoding='utf-8') as f: f.write(u"你好")

以上这篇python3.4.3下逐行读入txt文本并去重的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

  • Python做文本按行去重的实现方法
  • python逐行读写txt文件的实例讲解
相关热词搜索: python 逐行读入txt文本