Module object_to_csv
[hide private]
[frames] | no frames]

Source Code for Module object_to_csv

 1  #! /usr/local/bin/python 
 2  #-*- coding: utf-8 -*- 
 3   
 4  """object_to_csv 
 5   
 6  Loads a serialized graph object in memory and generates a CSV file. 
 7   
 8  CSV file format: 
 9   - source_ip;destination_ip;count 
10  """ 
11   
12  __author__ = "Jerome Hussenet, Cedric Bonhomme" 
13  __version__ = "$Revision: 0.2 $" 
14  __date__ = "$Date: 2009/02/19 $" 
15  __copyright__ = "Copyright (c) 2009 Jerome Hussenet, Copyright (c) 2009 Cedric Bonhomme" 
16  __license__ = "Python" 
17   
18  import os 
19  import sys 
20   
21  import csv 
22  import pickle 
23   
24 -class excel_french(csv.Dialect):
25 delimiter = ';' 26 quotechar = '"' 27 doublequote = True 28 skipinitialspace = False 29 lineterminator = '\n' 30 quoting = csv.QUOTE_MINIMAL
31 csv.register_dialect('excel_french', excel_french) 32
33 -def object_to_csv(obj_file, csv_file):
34 """Generate CSV file.""" 35 dic_obj = open(obj_file, "r") 36 if options.verbose: 37 print "Loading dictionary..." 38 dic_ip = pickle.load(dic_obj) 39 40 c = csv.writer(open(csv_file, "wb"), 'excel_french') 41 42 if options.verbose: 43 print "Writting CSV file..." 44 for ip_src in dic_ip: 45 for ip_dst in dic_ip[ip_src]: 46 c.writerow([ip_src, ip_dst, dic_ip[ip_src][ip_dst]])
47 48 49 if __name__ == "__main__": 50 # Point of entry in execution mode. 51 from optparse import OptionParser 52 parser = OptionParser() 53 parser.add_option("-i", "--input", dest="obj_file", 54 help="Python serialized object") 55 parser.add_option("-o", "--output", dest="csv_file", 56 help="CSV file") 57 parser.add_option("-q", "--quiet", 58 action="store_false", dest="verbose", 59 help="be vewwy quiet (I'm hunting wabbits)") 60 parser.set_defaults(obj_file = './data/dic.pyobj', 61 csv_file = './data/ip.csv', 62 verbose = True) 63 64 (options, args) = parser.parse_args() 65 66 object_to_csv(options.obj_file, options.csv_file) 67