2

In my python script, I write specific columns from a text_file to a new_text_file separated by , because the new_text_file will later become a csv_file. There are white space lines left over in the new_text_file because of lines I skipped writing over that need to be removed from the file.

I can't use .strip() or .rstrip() because I get the error: AttributeError: '_io.TextIOWrapper' object has no attribute 'strip'.

I can't use ip_file.write("".join(line for line in ip_file if not line.isspace())) because I get the error: UnsupportedOperation: not readable.

I also tried importing sys and re, and have tried every other answer found on this site, but it still returns errors.

My code is:

for ip in open("list.txt"):
    with open(ip.strip()+".txt", "a") as ip_file:
        for line in open("data.txt"):
            new_line = line.split(" ")
            if "blocked" in new_line:
                if "src="+ip.strip() in new_line:
                    #write columns to new text file
                    ip_file.write(", " + new_line[11])
                    ip_file.write(", " + new_line[12])
                    try:
                        ip_file.write(", " + new_line[14] + "\n")
                    except IndexError:
                        pass

The resulting ip_file looks like:

, dst=00.000.00.000, proto=TCP, dpt=80
, dst=00.000.00.000, proto=TCP, dpt=80
, dst=00.000.00.000, proto=TCP, dpt=80

, dst=00.000.00.000, proto=TCP, dpt=80
, dst=00.000.00.000, proto=TCP, dpt=80

I was coding under the last line of the above script, within the loops. The new_text_file is ip_file in my script and everything must be in Python.

Question: Is there another way to remove the blank lines in ip_file? OR prevent them from ever being written?

hjames
  • 439
  • 5
  • 12
  • 19
  • possible duplicate of [Remove empty lines](http://stackoverflow.com/questions/3711856/remove-empty-lines) –  Aug 22 '13 at 14:50
  • When you say "remove blank lines in `ip_file`", do you mean "avoid writing blank lines in `ip_file`"? I'm not understanding your question. You aren't reading from `ip_file` anywhere, so I don't see how you could be worried about reading blank lines from it, and all your calls to `ip_file.write` will at the minimum write a comma to the file. So can you clarify what you're asking? – Brionius Aug 22 '13 at 14:52
  • @Brionius: I'll edit my question a bit more. – hjames Aug 22 '13 at 14:55

1 Answers1

1

I think I understand what you're saying. Try making these changes:

        for line in open("data.txt"):
            new_line = line.rstrip().split()
                                    ^^^^^^^^^
            if "blocked" in new_line:
                if "src="+ip.strip() in new_line:
                    #write columns to new text file
                    ip_file.write(", " + new_line[11])
                    ip_file.write(", " + new_line[12])
                    try:
                        ip_file.write(", " + new_line[14])
            #                                                      ^^^^
                    except IndexError:
                        pass
                    ip_file.write("\n")
            #           

It seems that the problem was that when new_line[14] existed, it already contained a newline, so you were appending two newlines. The above code rstrips any newline off line before you split it, then appends a single newline no matter what at the end of the inner for loop.

Brionius
  • 13,858
  • 3
  • 38
  • 49