mdpoleto
mdpoleto

Reputation: 803

Open file indentation unexpected

I am a newcomer to developing in python I had a good search to see if I could answer my question prior to posting this, but my searches came up blank.

I am opening a file with random indentation and I want to search through it to find a specific line and write it in another file later on. For this, I am using:

with open("test.txt", "r+") as in_file:
buf = in_file.read().strip()
in_file.close()
out_file = open("output.txt", "w+")
for line in buf:
    if line.startswith("specific-line"):
        newline == line + "-found!"
        out_file.append(newline)
    out_file.close()

While my code loads and reads the file without any issues, the thing I'm struggling with is how to ignore the indentation in my "test.txt" file.

For example:

I might have.

ignore this line
ignore this line
specific-line one
specific-line two
ignore this line
    specific-line three
specific-line four
        specific-line five
ignore this line
ignore this line

in my file.

My code as it stands will only find the lines that start with 'specific-line' and have 'one', 'two' and 'four' in them.

What do I need to do to my code to change it, so that I also get the lines with 'specific-line' plus 'three' and 'five' in also, but while ignoring any other lines (marked as - 'ignore this line') that I don't want.

Can anyone help me?

Thanks! =]

Upvotes: 1

Views: 681

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122036

You have two problems, related to the way you read in the in_file. The line:

buf = in_file.read().strip()

will only strip whitespace from the start and end of the whole file, then:

for line in buf:

is actually iterating over characters. Also, you don't need to close if you use with.

Instead, try:

with open("test.txt") as in_file, open("output.txt", "w+") as out_file:
    for line in map(str.strip, in_file):
        if line.startswith(...):
            ...

Additionally, as Brionius points out in the comment, you're comparing (==) instead of assigning (=) to newline, which will cause a NameError.

Upvotes: 6

Related Questions