Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97413
| References | <muttuu$pc5$1@ger.gmane.org> <CAPTjJmrdE-n=VazddU7PZS6E=rbBf1XVoz_g2ArxK6454D5qxw@mail.gmail.com> |
|---|---|
| Date | 2015-10-06 00:51 +1100 |
| Subject | Re: Finding Blank Columns in CSV |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.391.1444053120.28679.python-list@python.org> (permalink) |
On Tue, Oct 6, 2015 at 12:48 AM, Chris Angelico <rosuav@gmail.com> wrote:
> fn = "tmp1.csv"
> fin = open(fn, 'rb')
> rdr = csv.DictReader(fin, delimiter=',')
> # all the same down to here
> blanks = set(rdr.fieldnames)
> for row in data:
> blanks = {col for col in blanks if not row[col]}
> mt = [col for col in rdr.fieldnames if col not in blanks]
> print mt
Oops, premature send - hadn't proofread it yet.
fn = "tmp1.csv"
fin = open(fn, 'rb')
rdr = csv.DictReader(fin, delimiter=',')
# all the same down to here
blanks = set(rdr.fieldnames)
for row in rdr:
blanks = {col for col in blanks if not row[col]}
mt = [col for col in rdr.fieldnames if col not in blanks]
print mt
Though I still haven't tested it, so there may be other bugs. Broadly
speaking, though, what it does is quite simple: start by assuming that
every column is nothing but blanks, and then any time you find a
non-blank cell, remove it from the set of blanks. At the end, all
field names not present in the set of blanks are non-blanks.
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Finding Blank Columns in CSV Chris Angelico <rosuav@gmail.com> - 2015-10-06 00:51 +1100
csiph-web