首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中复制没有重复和空白行的文件

在Python中复制没有重复和空白行的文件
EN

Stack Overflow用户
提问于 2016-11-30 16:42:50
回答 3查看 1.7K关注 0票数 2

我用Python编写了一段代码,将现有的文本文件(.txt)复制到同一位置的新文件(具有不同的名称)。这将按预期从原始文本文件复制所有文本:

代码语言:javascript
复制
a=open("file1.txt", "r") #existing file
b=open("file2.txt", "w") #file did not previously exist, hence "w"
for reform1 in a.readlines():
    b.write(reform1) #write the lines from 'reform1'
    reform1=a.readlines() #read the lines in the file
a.close() #close file a (file1)
b.close() #close file b (file2)

我现在被要求修改新的文件,从复制的文件中删除重复的行和空行(同时保留原稿),并保留其余的文本(唯一的行)。怎么做?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-30 16:56:31

这将向'file2.txt'写入'file1.txt'中的所有行,除了那些仅由空格组成或重复的行。该命令被保留,但假定只有重复的第一个实例才应该被写入:

代码语言:javascript
复制
seen = set()
with open('file1.txt') as f, open('file2.txt','w') as o:
    for line in f:
        if not line.isspace() and not line in seen:
            o.write(line)
            seen.add(line)

str.isspace()是所有空格(例如制表符)的True,而不仅仅是换行符,使用if not line == '\n'进行更严格的定义(假设没有'/r'换行符)。

我使用with语句处理文件的打开/关闭,并逐行读取文件,这是最重要的仿生方式。

对于在Python中复制文件,您应该使用shutil,如解释的here

票数 2
EN

Stack Overflow用户

发布于 2016-11-30 16:47:43

试试这个:

代码语言:javascript
复制
import re
a=open("file1.txt", "r") #existing file
b=open("file2.txt", "w") #file did not previously exist, hence "w"
exists = set()
for reform1 in a.readlines():
    if reform1 in exists:
        continue
    elif re.match(r'^\s$', reform1):
        continue
    else:
        b.write(reform1) #write the lines from 'reform1'
        exists.add(reform1)
a.close() #close file a (file1)
b.close() #close file b (file2)
票数 1
EN

Stack Overflow用户

发布于 2016-11-30 17:00:41

尝试:

代码语言:javascript
复制
a=open("file1.txt", "r") #existing file
b=open("file2.txt", "w") #file did not previously exist, hence "w"
seen = []
for reform1 in a.readlines():
    if reform1 not in seen and len(reform1) > 1:
        b.write(reform1) #write the lines from 'reform1'
        seen.append(reform1)
a.close() #close file a (file1)
b.close() #close file b (file2)

我使用"len(reform1) > 1“,因为当我创建测试文件时,空行有一个字符,大概是"\r”或"\n“字符。根据需要对您的应用程序进行调整。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40893689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档