Putting the return of my Python Script into a CSV file
<p>I have been recently working on a python script to look for missing reverse entries in our DNS servers on BIND and am having trouble figuring out a good way to able to return the output in preferably a CSV file.</p>

<p>Here's the code I am working with and I know it is rough around the edges right now. Any help / ideas would be very appreciated thank you.</p>

<pre><code>import dns.zone
import dns.ipv4
import os.path
import sys

reverse_map = {}

for file in sys.argv[1:]:
    zone = dns.zone.from_file(filename, os.path.basename(filename),relativize=False)
    for (name, ttl, rdata) in zone.iterate_rdatas('A'):
        l = reverse_map.get(rdata.address)
        if l is None:
            l = []
        reverse_map[rdata.address] = l
    l.append(name)
keys = reverse_map.keys()
keys.sort(lambda a1, a2: cmp(dns.ipv4.inet_aton(a1), dns.ipv4.inet_aton(a2)))
for k in keys:
v = reverse_map[k]
v.sort()
l = map(str, v)
print [k, l]


import csv

with open('csv.csv', 'rb') as f:
  data = list(csv.reader(f))

import collections
counter = collections.defaultdict(int)
for row in data:
    counter[row[0]] += 1


writer = csv.writer(open("/path/to/my/csv/file", 'w'))
for row in data:
    if counter[row[0]] &gt;= 4:
        writer.writerow(row)
</code></pre>

<p>Try <code>openpyxl</code> instead, that one seems to work better for me.</p>

<p><a href="https://openpyxl.readthedocs.io/en/default/" rel="nofollow" title="Documentation">Documentation</a></p>

python