Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'ideally': 0.04; 'string': 0.09; 'interpreted': 0.09; 'literal': 0.09; 'valueerror:': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'at.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integer,': 0.16; 'python;': 0.16; 'zero,': 0.16; 'zero.': 0.16; 'wrote:': 0.18; 'projects,': 0.19; 'unlike': 0.19; 'cc:addr:python.org': 0.22; 'print': 0.22; "aren't": 0.24; 'entries': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'feature': 0.29; 'change,': 0.30; 'newer': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'file': 0.32; 'languages': 0.32; '(most': 0.33; 'common': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; '14,': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'should': 0.36; 'pm,': 0.38; 'rather': 0.38; 'recent': 0.39; 'subject:can': 0.39; 'sure': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'blank': 0.60; 'new': 0.61; "you're": 0.61; 'more': 0.64; 'invalid': 0.68; 'default': 0.69; 'subject:this': 0.83; '10:': 0.84; 'cast': 0.91; 'to:none': 0.92; 'differences': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=zaaIEl4ptGdkJ2emppt5lXrlDAYUxfU3x0XrrcWdTfU=; b=GVFETZEeKg4gMx8GI5qyCYWJZTE95FN/SwUmyD8Jm6eyO03U0O1IBC5ke1pV+f25kw GsFQ1f4+7v2NlPsXgUfNxVQYkFD/WQuMJHnfdtY/uNiPhcusgb4XDSahffQKql/zp8e7 kGgSF58soRiSOPYSBnVIY5biZieFG2+OPN8DY0+84LbgOKcRJB+5guxRMH6yj4lR0JNg Nre12kmj4ZhLaQQ67O4+sov9J8jqNE8UcfxJ/ppAv2gZ4C7WpM9XyFKu47ox2plPu0VT D6UQV6V+w7xCskIpiijRvwEHAB4fmcT0J5T6Ii0nA7cvOu19jWGObnOzt2STc7Krit5m +43A== MIME-Version: 1.0 X-Received: by 10.68.222.8 with SMTP id qi8mr23880476pbc.16.1396756994751; Sat, 05 Apr 2014 21:03:14 -0700 (PDT) In-Reply-To: References: Date: Sun, 6 Apr 2014 14:03:14 +1000 Subject: Re: How can I parse this correctly? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396757395 news.xs4all.nl 2878 [2001:888:2000:d::a6]:43835 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69738 On Sun, Apr 6, 2014 at 1:52 PM, Anthony Papillion wrote: > When I try to > cast them like this: > > print int(row['YEAR']) > > I am told by the interpreter: > > Traceback (most recent call last): > File "analyze.py", line 14, in > print int(row['MONTH']) > ValueError: invalid literal for int() with base 10: '' > > What am I doing wrong? Am I not understanding HOW to cast? An empty string isn't a valid Python integer, unlike in some other languages where it's taken as zero. Do you have data in some but not in others? Should all blank entries be interpreted as zero? (That's common with a lot of spreadsheets.) Make sure that's really what you want, and then just do this: print int(row['MONTH'] or 0) That'll set a default of zero, if (and only if) the MONTH string is blank. By the way, is there a reason you're using Python 2 rather than Python 3? For new projects, you ideally should be working with a more recent version of Python; that way, you won't have to edit your code later, when you find there's some newer feature that you want. The differences aren't huge, but the sooner you make the change, the less code you have to look at. ChrisA