On Mon, 05 Oct 2015 13:29:03 +0000, Jaydip Chakrabarty wrote: > Hello, > > I have a csv file like this. > > Name,Surname,Age,Sex abc,def,,M ,ghi,,F jkl,mno,, > pqr,,,F > > I want to find out the blank columns, that is, fields where all the > values are blank. Here is my python code. > > fn = "tmp1.csv" > fin = open(fn, 'rb') > rdr = csv.DictReader(fin, delimiter=',') > data = list(rdr) > flds = rdr.fieldnames fin.close() > mt = [] > flag = 0 for i in range(len(flds)): > for row in data: > if len(row[flds[i]]): > flag = 0 break > else: > flag = 1 > if flag: > mt.append(flds[i]) flag = 0 > print mt > > I need to know if there is better way to code this. > > Thanks.
Assuming all the records have the same number of fields: I'd create a list of flags of length numfields, all set to 0 then for each record, I*d set flag[n] = 1 if field[n] has content then I'd check if I still have any 0 flags, and if I do, process the next record As soon as I have no 0 flags, I can stop processing records, as this means I have no empty columns. It might be more efficient if, when checking a record, I only tested the fields for which flag was still 0. Example (untested) flags = [False for x in rdr.fieldnames] for row in data: blanks = False for i in range(len(flags)): if not flags[i]: if len(row[i]) == 0: flags[i] = True else: blanks = True if not blanks: break -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list