In my py script, I need to write data into a csv file with the result of five columns per row.
title:info title title title title
one two three four five
one two three four five
title:info title title title title
one two three four five
I want to use the file.write() method, not the csv module if at all possible.
for ip in open("list.txt"):
with open("data.txt", "a") as csv_file:
csv_file.write("\r Title:" + ip + ", Title, Title, Title, Title \r")
for line in open("0820-oRG-apflog.txt"):
new_line = line.split()
#search apflog for correct lines
if "blocked" in new_line:
if "src="+ip.strip() in new_line:
#write columns to new text file & remove headers from lines in files and add commas
csv_file.write(ip + ", " + new_line[11].replace("dst=", ", ") + new_line[12].replace("proto=", ", "))
try:
csv_file.write(new_line[14].replace("dpt=", ", "))
except IndexError:
pass
When I run the script, I get the result:
title:info
title title title title
one
two three four five
I have tried: csv_file.write('%r\n%r\n%r\n' % (one, three, four)) and variations, but the data just isn't working the way I need it to.
I don't know where my code is wrong. I thought that .write() would write data to the same row unless otherwise specified (from previous uses).
Question: How do I .write() one line of the pattern into one csv row, and still separate the pattern into different rows?
Thanks!