2

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': '[email protected]', 'abuse_emails': '[email protected]', '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': '[email protected]', 'abuse_emails': '[email protected]', '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)

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Output in first cell is 4.4.4.4 "[{'updated': '2012....... is there a way to add a second delimiter " (double quote)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.