interactive window vs. script: inconsistent behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bell, Kevin

    #1

    interactive window vs. script: inconsistent behavior

    The following works in the interactive window of PythonWin, but fails in
    a script. TypeError: Objects of type 'slice' can not be converted to a
    COM VARIANT

    I just need to parse out these dates, but it's making me crazy.
    Shouldn't it work in both the interactive window and a script?

    [color=blue][color=green][color=darkred]
    >>> d = "5-18-05 to 5-31-05"
    >>> print d[0:d.find("to")-1][/color][/color][/color]
    5-18-05[color=blue][color=green][color=darkred]
    >>> print d[d.find("to")+3:][/color][/color][/color]
    5-31-05[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]




    Kev


  • Duncan Booth

    #2
    Re: interactive window vs. script: inconsistent behavior

    Bell, Kevin wrote:
    [color=blue]
    > The following works in the interactive window of PythonWin, but fails in
    > a script. TypeError: Objects of type 'slice' can not be converted to a
    > COM VARIANT
    >
    > I just need to parse out these dates, but it's making me crazy.
    > Shouldn't it work in both the interactive window and a script?
    >
    >[color=green][color=darkred]
    >>>> d = "5-18-05 to 5-31-05"
    >>>> print d[0:d.find("to")-1][/color][/color]
    > 5-18-05[color=green][color=darkred]
    >>>> print d[d.find("to")+3:][/color][/color]
    > 5-31-05[color=green][color=darkred]
    >>>>[/color][/color]
    >[/color]

    Not surprisingly, the above three lines work perfectly well in a script:

    d = "5-18-05 to 5-31-05"
    print d[0:d.find("to")-1]
    print d[d.find("to")+3:]

    and the output is:
    C:\temp>t.py
    5-18-05
    5-31-05

    Perhaps if you were to post an actual script which doesn't work and the
    actual error it generates, it might be possible to help you.

    Comment

    • Larry Bates

      #3
      Re: interactive window vs. script: inconsistent behavior

      This also works and IMHO reads better:

      d="5-18-05 to 5-31-05"
      f,t=d.split(' to '))

      Larry Bates

      Bell, Kevin wrote:[color=blue]
      > The following works in the interactive window of PythonWin, but fails in
      > a script. TypeError: Objects of type 'slice' can not be converted to a
      > COM VARIANT
      >
      > I just need to parse out these dates, but it's making me crazy.
      > Shouldn't it work in both the interactive window and a script?
      >
      >
      >[color=green][color=darkred]
      >>>>d = "5-18-05 to 5-31-05"
      >>>>print d[0:d.find("to")-1][/color][/color]
      >
      > 5-18-05
      >[color=green][color=darkred]
      >>>>print d[d.find("to")+3:][/color][/color]
      >
      > 5-31-05
      >
      >
      >
      >
      >
      > Kev
      >
      >[/color]

      Comment

      Working...