开发者

How to delete parts of a file in python?

I have a file named a.txt which looks like this:

I'm the first line

I'm the second line.

There may be more lines here.

I'm below an empty line.

I'm a line.

More lines here.

Now开发者_运维技巧, I want to remove the contents above the empty line(including the empty line itself). How could I do this in a Pythonic way?


Basically you can't delete stuff from the beginning of a file, so you will have to write to a new file.

I think the pythonic way looks like this:

# get a iterator over the lines in the file:
with open("input.txt", 'rt') as lines:
    # while the line is not empty drop it
    for line in lines:
        if not line.strip():
            break

    # now lines is at the point after the first paragraph
    # so write out everything from here
    with open("output.txt", 'wt') as out:
        out.writelines(lines)

Here are some simpler versions of this, without with for older Python versions:

lines = open("input.txt", 'rt')
for line in lines:
    if not line.strip():
        break
open("output.txt", 'wt').writelines(lines)

and a very straight forward version that simply splits the file at the empty line:

# first, read everything from the old file
text = open("input.txt", 'rt').read()

# split it at the first empty line ("\n\n")
first, rest = text.split('\n\n',1)

# make a new file and write the rest
open("output.txt", 'wt').write(rest)

Note that this can be pretty fragile, for example windows often uses \r\n as a single linebreak, so a empty line would be \r\n\r\n instead. But often you know the format of the file uses one kind of linebreaks only, so this could be fine.


Naive approach by iterating over the lines in the file one by one top to bottom:

#!/usr/bin/env python

with open("4692065.txt", 'r') as src, open("4692065.cut.txt", "w") as dest:
    keep = False
    for line in src:
        if keep: dest.write(line)
        if line.strip() == '': keep = True


The fileinput module (from the standard library) is convenient for this kind of thing. It sets things up so you can act as though your are editing the file "in-place":

import fileinput
import sys

fileobj=iter(fileinput.input(['a.txt'], inplace=True))
# iterate through the file until you find an empty line.
for line in fileobj:
    if not line.strip():
        break
# Iterators (like `fileobj`) pick up where they left off. 
# Starting a new for-loop saves you one `if` statement and boolean variable.
for line in fileobj:
    sys.stdout.write(line)


Any idea how big the file is going to be?

You could read the file into memory:

f = open('your_file', 'r')
lines = f.readlines()

which will read the file line by line and store those lines in a list (lines).

Then, close the file and reopen with 'w':

f.close()
f = open('your_file', 'w')
for line in lines:
    if your_if_here:
        f.write(line)

This will overwrite the current file. Then you can pick and choose which lines from the list you want to write back in. Probably not a very good idea if the file gets to large though, since the entire file has to reside in memory. But, it doesn't require that you create a second file to dump your output.


from itertools import dropwhile, islice

def content_after_emptyline(file_object):
    return islice(dropwhile(lambda line: line.strip(), file_object), 1, None)

with open("filename") as f:
    for line in content_after_emptyline(f):
        print line,


You could do a little something like this:

with open('a.txt', 'r') as file:
    lines = file.readlines()

blank_line = lines.index('\n')
lines = lines[blank_line+1:] #\n is the index of the blank line

with open('a.txt', 'w') as file:
    file.write('\n'.join(lines))

and that makes the job much simpler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜