Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'subject:How': 0.09; 'exists,': 0.09; 'optional.': 0.09; 'cc:addr:python-list': 0.10; '(the': 0.15; 'cases': 0.15; 'messy': 0.16; 'sequence.': 0.16; 'simplest': 0.16; 'specified.': 0.16; 'subject:exists': 0.16; 'wrote:': 0.17; 'skip': 0.17; 'define': 0.20; 'bit': 0.21; 'required.': 0.22; 'work,': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'elements': 0.23; 'split': 0.23; 'cc:no real name:2**0': 0.24; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'instead.': 0.27; 'lines': 0.28; 'initial': 0.28; 'end,': 0.29; 'probably': 0.29; 'ends': 0.30; 'field,': 0.30; 'error': 0.30; 'gets': 0.32; 'file': 0.32; 'could': 0.32; 'print': 0.32; 'another': 0.33; 'text': 0.34; 'list': 0.35; 'exist': 0.35; 'subject:?': 0.35; 'something': 0.35; 'there': 0.35; 'list.': 0.35; 'add': 0.36; 'but': 0.36; 'compare': 0.36; 'thank': 0.36; 'beyond': 0.37; 'does': 0.37; 'being': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'nothing': 0.38; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'end': 0.40; 'day,': 0.60; 'you.': 0.61; 'middle': 0.66; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'descriptive': 0.84; 'everything,': 0.84; 'inclusive': 0.84; 'subscripts': 0.84; 'approach.': 0.91; 'angel': 0.93 Date: Sat, 08 Sep 2012 07:08:09 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: tinnews@isbd.co.uk Subject: Re: How to print something only if it exists? References: <9s4nh9-8dr.ln1@chris.zbmc.eu> <6nhrh9-908.ln1@chris.zbmc.eu> In-Reply-To: <6nhrh9-908.ln1@chris.zbmc.eu> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:XrNZvIywlxihn0BvWwzZIMlMK19CB/st/guOA944rIf FEMEGIMUNElRxi06vFbs5oimpUh8DaCCkj4r24+q5rSOjbhf7e 6XmDIErG6DuFqhBE+EcQilGM31vTSBoPp+TLF0DyRxa6Q1Kj/K ilxCkpPxd7mwaqpeoQq+jejVpvyA3Io+WlzjsqZnzuyOwYD6Nn vA5qtHieRvReSV1ET4BIIGRqIAYqYWL4v4rK5OEURpRqn2P6Ze ypE83YD0YNKja33dU8OTh1/+yBOBf3YwqJ3XIxVB+rPUZCv8OL PDvhaidGkZHXnZS5bLPb3Ro45BzTZADAMoLV+c4cgCi5AlvtA= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347102512 news.xs4all.nl 6922 [2001:888:2000:d::a6]:33624 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28718 On 09/08/2012 06:02 AM, tinnews@isbd.co.uk wrote: > Dave Angel wrote: >> Would you like to define "exists" ? A list is not sparse, so all items >> exist if their subscript is less than the length of the list. So all >> you need to do is compare 2 to len(fld). >> > Yes, a I said a simple len(fld) will tell me if fld[2] 'exists' but it > gets messy if I have to do it in the middle of the print sequence. > > >> But perhaps there's another approach. Just what DO you want to print if >> fld(1) exists, but fld(2) does not? Do you still want to print out day, >> fld(1), and balance? Or do you want to skip balance as well? >> > Here's a sample of the file whose lines are being split() :- > > 01 JB 0.00 Start of 2012, Initial balance > 02 BB 0.00 > 13 ZB 0.00 > > I want to print out everything, it's just that in some cases there's > no descriptive text (the bit that ends up in fld[2]). > > >> if you literally want nothing printed for list elements beyond the end, >> then I'd add some extra empty-strings to the end of the list. >> >> fld.extend("" * 5) >> >> Now, subscripts 0 through 4 inclusive will work, as specified. >> > That's probably the simplest approach, thank you. > If there literally is only one missing field, and at the end, then you could use fld.append("") instead. Or better, you could do something like if len(fld) == 2 : fld.append(""") This may be longer, but at least it's in one place -- the place where the split is occurring. And it pretty much states that the fld2 is optional. You ought to consider what error to report if you encounter a line with missing fields that ARE required. -- DaveA