Python files (CSV data)


Libraries:
CSV and JSON

CSV Methods:
reader() –> returns a reader object. Each row read from the csv file is returned as a list of strings. No automatic data type conversion is performed.
DictReader() –> returns a writer object responsible for converting the data into delimited strings
writer() –>

CSV Example:

Sample of the data:
datetime,host,src,proto,type,spt,dpt
2013-03-03 21:53:59,groucho-oregon,1032051418,TCP,,6000,1433

import csv

count = 0

with open(“filename”) as FH:
   datareader = csv.reader(FH, delimiter=’,’) # returns an object
      for row in datareader: # each row is a list
         if count == 0: # header row
            count += 1
         elif count > 0 and count < 5:
            data_time = row[0]
            print(data_time.split(‘,’))
            count += 1
         else:
            break