Path: csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.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; 'column': 0.07; 'data:': 0.07; 'indices': 0.07; "'rb')": 0.09; 'csv': 0.09; 'subject:CSV': 0.09; 'python': 0.10; 'def': 0.13; '(len': 0.16; '10/05/2015': 0.16; 'columns': 0.16; 'len': 0.16; 'received:195.186': 0.16; 'received:bluewin.ch': 0.16; 'row': 0.16; 'simpler,': 0.16; 'wrote:': 0.16; 'thanks.': 0.18; 'code.': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'this.': 0.28; 'values': 0.28; 'print': 0.30; 'code': 0.30; 'operations': 0.31; 'table': 0.32; 'file': 0.34; 'lists.': 0.35; 'skip:i 20': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'data': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'hello,': 0.40; 'charset:windows-1252': 0.62; 'skip:n 10': 0.62; 'here': 0.66; 'received:ch': 0.66; 'fin': 0.84 Subject: Re: Finding Blank Columns in CSV To: python-list@python.org References: From: Friedrich Rentsch Date: Mon, 5 Oct 2015 22:08:33 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1444075780 news.xs4all.nl 23828 [2001:888:2000:d::a6]:33107 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97424 On 10/05/2015 03:29 PM, 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. > Operations on columns are often simpler, if a table is rotated beforehand. Columns become lists. def find_empty_columns (table): number_of_records = len (table) rotated_table = zip (*table) indices_of_empty_columns = [] for i in range (len (rotated_table)): # Column indices if rotated_table[i].count ('') == number_of_records: indices_of_empty_columns.append (i) return indices_of_empty_columns Frederic