Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97425
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Finding Blank Columns in CSV |
| Date | 2015-10-05 20:56 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <muuo6k$q4o$1@dont-email.me> (permalink) |
| References | <mailman.389.1444052108.28679.python-list@python.org> |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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