Path: csiph.com!news.mixmin.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'data:': 0.07; "'rb')": 0.09; 'csv': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:CSV': 0.09; 'python': 0.10; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'row': 0.16; 'thanks.': 0.18; 'code.': 0.23; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'this.': 0.28; 'values': 0.28; 'print': 0.30; 'code': 0.30; 'file': 0.34; 'there': 0.36; 'to:addr:python-list': 0.36; 'received:org': 0.37; 'data': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'hello,': 0.40; 'here': 0.66; 'received:116': 0.81; 'fin': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Jaydip Chakrabarty Subject: Finding Blank Columns in CSV Date: Mon, 5 Oct 2015 13:29:03 +0000 (UTC) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 116.193.139.6 User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1444052108 news.xs4all.nl 23826 [2001:888:2000:d::a6]:60325 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97411 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.