Python newbie question re Strings and integers
Christian Heimes
lists at cheimes.de
Thu Sep 18 19:36:46 EDT 2008
rmac wrote:
>
> 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
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.
http://docs.python.org/lib/module-os.path.html
import os.path
filename = os.path.basename(path)
prefix, extension = os.path.splitext(filename)
Christian
More information about the Python-list
mailing list