Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97411

Finding Blank Columns in CSV

From Jaydip Chakrabarty <chalao.adda@gmail.com>
Subject Finding Blank Columns in CSV
Date 2015-10-05 13:29 +0000
Newsgroups comp.lang.python
Message-ID <mailman.389.1444052108.28679.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Finding Blank Columns in CSV Jaydip Chakrabarty <chalao.adda@gmail.com> - 2015-10-05 13:29 +0000
  Re: Finding Blank Columns in CSV Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-05 20:56 +0000

csiph-web