Python newbie question re Strings and integers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rmac

    Python newbie question re Strings and integers



    the following code attempts to extract a symbol name from a string:
    extensionStart = int(filename.rf ind('.'))
    filenameStart = int(filename.rf ind('/'))
    #print 'Extension Start - ' + str(extensionSt art)
    #print 'FileName Start - ' + str(filenameSta rt)
    currentSymbol=f ilename[int(filenameSta rt),int(extensi onStart)]

    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=f ilename[int(filenameSta rt),int(extensi onStart)]
    TypeError: string indices must be integers

    Running Python 2.5.2_5 on OSX
  • Miki

    #2
    Re: Python newbie question re Strings and integers

        currentSymbol=f ilename[int(filenameSta rt),int(extensi onStart)]
    Should be
    currentSymbol=f ilename[int(filenameSta rt):int(extensi onStart)]
    (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(basena me(filename))[0]

    HTH,
    --
    Miki <miki.tebeka@gm ail.com>
    If it won't be simple, it simply won't be. [Hire me, source code]

    Comment

    • Christian Heimes

      #3
      Re: Python newbie question re Strings and integers

      rmac wrote:
      >
      the following code attempts to extract a symbol name from a string:
      extensionStart = int(filename.rf ind('.'))
      filenameStart = int(filename.rf ind('/'))
      #print 'Extension Start - ' + str(extensionSt art)
      #print 'FileName Start - ' + str(filenameSta rt)
      currentSymbol=f ilename[int(filenameSta rt),int(extensi onStart)]
      >
      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=f ilename[int(filenameSta rt),int(extensi onStart)]
      TypeError: string indices must be integers
      You are using , inside filename[]. The splicing syntax is start:end, not
      start,end.

      You are better off with using the appropriate APIs from the os.path module.

      Source code: Lib/genericpath.py, Lib/posixpath.py(for POSIX) and Lib/ntpath.py(for Windows). This module implements some useful functions on pathnames. To read or write files see open(), and for ac...


      import os.path
      filename = os.path.basenam e(path)
      prefix, extension = os.path.splitex t(filename)

      Christian

      Comment

      • rmac

        #4
        Re: Python newbie question re Strings and integers


        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.

        Comment

        • Bruno Desthuilliers

          #5
          Re: Python newbie question re Strings and integers

          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 !-)

          Comment

          Working...