JRR
JRR

Reputation: 317

python 2.x csv writer

When I print these strings

print query, netinfo

I get output below, which is fine. How do i take these strings and put them into a CSV file into a single row?

8.8.8.8 [{'updated': '2012-02-24T00:00:00', 'handle': 'NET-8-0-0-0-1', 'description': 'Level 3 Communications, Inc.', 'tech_emails': 'ipaddressing@level3.com', 'abuse_emails': 'abuse@level3.com', 'postal_code': '80021', 'address': '1025 Eldorado Blvd.', 'cidr': '8.0.0.0/8', 'city': 'Broomfield', 'name': 'LVLT-ORG-8-8', 'created': '1992-12-01T00:00:00', 'country': 'US', 'state': 'CO', 'range': '8.0.0.0 - 8.255.255.255', 'misc_emails': None}, {'updated': '2014-03-14T00:00:00', 'handle': 'NET-8-8-8-0-1', 'description': 'Google Inc.', 'tech_emails': 'arin-contact@google.com', 'abuse_emails': 'arin-contact@google.com', 'postal_code': '94043', 'address': '1600 Amphitheatre Parkway', 'cidr': '8.8.8.0/24', 'city': 'Mountain View', 'name': 'LVLT-GOGL-8-8-8', 'created': '2014-03-14T00:00:00', 'country': 'US', 'state': 'CA', 'range': None, 'misc_emails': None}]

I have tried hobbling this together but it's all jacked up. I could use some help on how to use the csv module.

writer=csv.writer(open('dict.csv', 'ab'))
for key in query:
   writer.writerow(query)

Upvotes: 2

Views: 113

Answers (1)

Kasravnd
Kasravnd

Reputation: 107347

You can put your variables in a tuple and write to csv file :

import csv
from operator import itemgetter
with open('ex.csv', 'wb') as csvfile:
     spamreader = csv.writer(csvfile, delimiter=' ')
     spamreader.writerow((query, netinfo))

Note: if you are in python 3 use following code :

import csv
from operator import itemgetter
with open('ex.csv', 'w',newline='') as csvfile:
     spamreader = csv.writer(csvfile, delimiter=' ')
     spamreader.writerow((query, netinfo))

Upvotes: 1

Related Questions