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


Groups > comp.lang.python > #97411 > unrolled thread

Finding Blank Columns in CSV

Started byJaydip Chakrabarty <chalao.adda@gmail.com>
First post2015-10-05 13:29 +0000
Last post2015-10-05 20:56 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#97411 — Finding Blank Columns in CSV

FromJaydip Chakrabarty <chalao.adda@gmail.com>
Date2015-10-05 13:29 +0000
SubjectFinding Blank Columns in CSV
Message-ID<mailman.389.1444052108.28679.python-list@python.org>
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.

[toc] | [next] | [standalone]


#97425

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-10-05 20:56 +0000
Message-ID<muuo6k$q4o$1@dont-email.me>
In reply to#97411
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, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web