python - empty key while using dictread -


i using dictread read cvs file. problem files empty key. have 6 keys:below code , file:

from datetime import datetime math import sqrt, exp, log csv import dictreader import pandas pd import numpy np  train = '/users/mas/documents/workspace/avito/input/minitrain.csv'  t,row in enumerate(dictreader(open(train))):     pass  print row 

this output

{'': none, 'searchid': '4', 'isclick': none, 'histctr': '', 'adid': '24129570', 'position': '2', 'objecttype': '2'} 

this cvs file

searchid,adid,position,objecttype,histctr,isclick, 2,11441863,1,3,0.001804,0, 2,22968355,7,3,0.004723,0, 3,212187,7,3,0.029701,0, 3,34084553,1,3,0.004300,0, 3,36256251,2,2,,, 4,2073399,6,1,,, 4,6046052,7,1,,, 4,17544913,8,1,,, 4,20653823,1,3,0.003049,0, 4,24129570,2,2,,, 

whay getting empty key?!

try set fieldnames when read csv file:
dictreader(open(train), fieldnames=('searchid', 'adid', 'position', 'objecttype', 'histctr', 'isclick',))

you write own dictreader based on dictreader:

class mydictreader():     def __init__(self, f, fieldnames=none, dialect='excel', *args, **kwrags):         self.reader = csv.reader(f, dialect, *args, **kwrags)         self._fieldnames = fieldnames         if self._fieldnames none:             try:                 self._fieldnames = next(self.reader)             except stopiteration:                 pass      def __iter__(self):         return self      def next(self):         d = {}         row = self.reader.next()         index, fieldname in enumerate(self._fieldnames):             if fieldname:                 d[fieldname] = row[index]         return d 

and after using it:

for t, row in enumerate(mydictreader(open(train))):     pass     print row 

you output without empty key:

{'searchid': '4', 'isclick': '', 'histctr': '', 'adid': '24129570', 'position': '2', 'objecttype': '2'} 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -