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?