Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69739 > unrolled thread

Re: How can I parse this correctly?

Started byBen Finney <ben+python@benfinney.id.au>
First post2014-04-06 14:21 +1000
Last post2014-04-06 14:21 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How can I parse this correctly? Ben Finney <ben+python@benfinney.id.au> - 2014-04-06 14:21 +1000

#69739 — Re: How can I parse this correctly?

FromBen Finney <ben+python@benfinney.id.au>
Date2014-04-06 14:21 +1000
SubjectRe: How can I parse this correctly?
Message-ID<mailman.8934.1396758100.18130.python-list@python.org>
Anthony Papillion <papillion@gmail.com> 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 <module>
>     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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web