Use Python to remove lines in a files that start w

2019-03-15 07:46发布

This seems like a straight-forward question but I can't seem to pinpoint my problem. I am trying to delete all lines in a file that start with an octothorpe (#) except the first line. Here is the loop I am working with:

for i, line in enumerate(input_file):
    if i > 1:
        if not line.startswith('#'):
            output.write(line)

The above code doesn't seem to work. Does anyone known what my problem is? Thanks!

3条回答
三岁会撩人
2楼-- · 2019-03-15 08:34

I wouldn't bother with enumerate here. You only need it decide which line is the first line and which isn't. This should be easy enough to deal with by simply writing the first line out and then using a for loop to conditionally write additional lines that do not start with a '#'.

def removeComments(inputFileName, outputFileName):

    input = open(inputFileName, "r")
    output = open(outputFileName, "w")

    output.write(input.readline())

    for line in input:
        if not line.lstrip().startswith("#"):
            output.write(line)

    input.close()
    output.close()

Thanks to twopoint718 for pointing out the advantage of using lstrip.

查看更多
对你真心纯属浪费
3楼-- · 2019-03-15 08:34

Maybe you want to omit lines from the output where the first non-whitespace character is an octothorpe:

for i, line in enumerate(input_file):
    if i == 0 or not line.lstrip().startswith('#'):
        output.write(line)

(note the call to lstrip)

查看更多
仙女界的扛把子
4楼-- · 2019-03-15 08:39

You aren't writing out the first line:

for i, line in enumerate(input_file):
    if i == 0:
        output.write(line)
    else:
        if not line.startswith('#'):
            output.write(line)

Keep in mind also that enumerate (like most things) starts at zero.

A little more concisely (and not repeating the output line):

for i, line in enumerate(input_file):
    if i == 0 or not line.startswith('#'):
        output.write(line)
查看更多
登录 后发表回答