Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97414 > unrolled thread
| Started by | Random832 <random832@fastmail.com> |
|---|---|
| First post | 2015-10-05 10:00 -0400 |
| Last post | 2015-10-05 10:00 -0400 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Finding Blank Columns in CSV Random832 <random832@fastmail.com> - 2015-10-05 10:00 -0400
| From | Random832 <random832@fastmail.com> |
|---|---|
| Date | 2015-10-05 10:00 -0400 |
| Subject | Re: Finding Blank Columns in CSV |
| Message-ID | <mailman.392.1444053638.28679.python-list@python.org> |
On Mon, Oct 5, 2015, at 09:29, 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. Well, for one thing, you should just be using something like "for fld in flds" rather than range(len(...)). There's also an opportunity to use list/generator comprehensions instead of explicit loops - your whole loop could be written as: mt = [fld for fld in flds if not any(row[fld] for row in data)] It might be more efficient to iterate over rows first than columns, depending on your data (if you have a large number of rows, consider that you're reading them all into memory at the start) This would be more complex code, though, and more difficult to write as a comprehension, so if your data isn't ideal for it it might not be worth doing.
Back to top | Article view | comp.lang.python
csiph-web