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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'string.': 0.05; 'ignored': 0.07; 'literal': 0.09; 'okay': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'valueerror:': 0.09; 'subject:How': 0.10; 'python': 0.11; 'assume': 0.14; '(empty': 0.16; '8bit%:32': 0.16; 'fine.': 0.16; 'finney': 0.16; 'guess.': 0.16; 'integers.': 0.16; 'objects.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'unambiguous': 0.16; 'all.': 0.16; 'year,': 0.18; 'module': 0.19; 'print': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'case.': 0.24; 'integer': 0.24; 'string,': 0.24; 'decide': 0.24; 'this:': 0.26; 'header:X -Complaints-To:1': 0.27; "doesn't": 0.30; 'writes:': 0.31; 'file': 0.32; '(most': 0.33; '14,': 0.36; 'dates': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'ben': 0.38; 'handle': 0.38; 'to:addr:python- list': 0.38; 'recent': 0.39; 'expect': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'free': 0.61; 'new': 0.61; "you're": 0.61; "you'll": 0.62; "you've": 0.63; 'needing': 0.65; 'skip:\xe2 10': 0.65; 'invalid': 0.68; 'press': 0.70; 'led': 0.72; '8bit%:43': 0.74; 'subject:this': 0.83; '10:': 0.84; 'received:125': 0.84; 'refuses': 0.84; 'cast': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: How can I parse this correctly? Date: Sun, 06 Apr 2014 14:21:27 +1000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xBD41714B X-Public-Key-Fingerprint: 9CFE 12B0 791A 4267 887F 520C B7AC 2E51 BD41 714B X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-gpg.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:ZZ4oCApdpV2lH0FCtzVCvLzxbNU= 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396758100 news.xs4all.nl 2836 [2001:888:2000:d::a6]:46524 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69739 Anthony Papillion writes: > for row in r: > print row['YEAR'] > > This works fine. But, I am needing to do date addition/subtraction > using datetime and so I need these dates as integers. I assume you mean you will be creating ‘datetime.date’ objects. What will you set as the month and day? Alternatively, if you just want to do integer arithmetic on the year, you don't need the ‘datetime’ module at all. > When I try to cast them like this: Python doesn't have “cast”; instead, you request the creation of a new object by calling the type. > print int(row['YEAR']) What do you expect this to return when ‘row['YEAR']’ is ‘""’ (empty string)? > 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? You've ignored the condition where your ‘row['YEAR']’ is the empty string. Python doesn't have an unambiguous integer represented by the empty string, so it refuses to guess. You'll need to handle that specially, and decide what value you want when that's the case. -- \ “A free press is one where it's okay to state the conclusion | `\ you're led to by the evidence.” —Bill Moyers | _o__) | Ben Finney