Python newbie question re Strings and integers

Discussion in 'Python' started by rmac, Sep 19, 2008.

  1. rmac

    rmac Guest

    the following code attempts to extract a symbol name from a string:
    extensionStart = int(filename.rfind('.'))
    filenameStart = int(filename.rfind('/'))
    #print 'Extension Start - ' + str(extensionStart)
    #print 'FileName Start - ' + str(filenameStart)
    currentSymbol=filename[int(filenameStart),int(extensionStart)]

    Uncommenting the print statements clearly show the values to be
    integers (and without the str casts actually provide int+string
    errors)

    However, executing this code results in...
    opening - /Users/rmac/Documents/Sandbox/data/MarketData/AA.csv
    Traceback (most recent call last):
    File "rHistFileToDB_Equities.py", line 25, in <module>
    currentSymbol=filename[int(filenameStart),int(extensionStart)]
    TypeError: string indices must be integers

    Running Python 2.5.2_5 on OSX
     
    rmac, Sep 19, 2008
    #1
    1. Advertising

  2. rmac

    Miki Guest

    >     currentSymbol=filename[int(filenameStart),int(extensionStart)]
    Should be
    currentSymbol=filename[int(filenameStart):int(extensionStart)]
    (change , to :)

    You don't need to convert to int all the time, rfind will return an
    integer.

    Also you can use os.path for this

    from os.path import basename, splitext
    currentSymbol = splitext(basename(filename))[0]

    HTH,
    --
    Miki <>
    http://pythonwise.blogspot.com
     
    Miki, Sep 19, 2008
    #2
    1. Advertising

  3. rmac

    rmac Guest

    Ah! Arghh!!! You are so correct on the usage of the ':'

    Python syntax is a little different from what I am used to.

    Thank you.
     
    rmac, Sep 19, 2008
    #3
  4. rmac a écrit :
    >
    > the following code attempts to extract a symbol name from a string:
    > extensionStart = int(filename.rfind('.'))


    rfind returns an int, so passing it to the int type constructor is useless.

    > filenameStart = int(filename.rfind('/'))


    idem

    > #print 'Extension Start - ' + str(extensionStart)


    The print statement isn't restricted to string objects. And FWIW, you
    can use string formating. The above should be either

    print "Extension start - ", extensionStart

    or

    print "Extension start - %s" % extensionStart

    As a side not, the naming convention in Python is to use all_lower for
    identifiers.

    > #print 'FileName Start - ' + str(filenameStart)


    idem.

    > currentSymbol=filename[int(filenameStart),int(extensionStart)]


    Two more useless int constructor calls.

    > Uncommenting the print statements clearly show the values to be
    > integers


    If by 'the values', you mean objects bound to identifiers
    'filenameStart' and 'extensionStart', I fail to see how they could be
    anything else...

    > (and without the str casts actually provide int+string
    > errors)


    Indeed. What should be the semantic of expression
    'answer is ' + 42

    ???

    > However, executing this code results in...
    > opening - /Users/rmac/Documents/Sandbox/data/MarketData/AA.csv
    > Traceback (most recent call last):
    > File "rHistFileToDB_Equities.py", line 25, in <module>
    > currentSymbol=filename[int(filenameStart),int(extensionStart)]
    > TypeError: string indices must be integers


    the expression:

    int(filenameStart),int(extensionStart)

    1/ takes the object currently bound to identifier 'filenameStart' and
    pass it to the int type constructor

    2/ takes the object currently bound to identifier 'extensionStart' and
    pass it to the int type constructor

    3/ build a tuple (actually, a pair of ints) from the results of the two
    above expressions



    Then you try to use this tuple as an index on a string. A tuple is not a
    legal type for string (or any other sequence) subscripting.

    The complete syntax for string subscripting is described in the
    FineManual. To make a long story short, it's :

    string[start:end:step]

    where end and step are optionals.

    IOW, the correct syntax here is:
    filename[filenameStart:extensionStart]


    Now, while this is the correct *syntax*, it's not the correct *idiom*.
    There's a module named os.path, which provides the needed service. I
    leave it up to you to read the relevant part of the documentation...
     
    Bruno Desthuilliers, Sep 20, 2008
    #4
  5. rmac a écrit :
    > Ah! Arghh!!! You are so correct on the usage of the ':'
    >
    > Python syntax is a little different from what I am used to.


    I don't know what you're used to, but chances are that more than the
    syntax differs !-)
     
    Bruno Desthuilliers, Sep 22, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    449
    Roy Smith
    May 20, 2004
  2. Replies:
    0
    Views:
    363
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
  4. Frederik
    Replies:
    9
    Views:
    2,635
    Kevin McMurtrie
    Oct 15, 2009
  5. Paul
    Replies:
    8
    Views:
    180
    David A. Black
    Mar 18, 2007
Loading...

Share This Page