Python newbie question re Strings and integers

R

rmac

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
 
M

Miki

    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,
 
R

rmac

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.
 
B

Bruno Desthuilliers

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...
 
B

Bruno Desthuilliers

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top