473,287 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Python newbie question re Strings and integers



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
Sep 18 '08 #1
4 993
* * 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 <mi*********@gmail.com>
http://pythonwise.blogspot.com
Sep 18 '08 #2
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

Sep 18 '08 #3

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.
Sep 18 '08 #4
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 !-)

Sep 22 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: John Fabiani | last post by:
I have just installed SUSE 9.1 64 and it created a /usr/lib64/python2.3/. Note the 'lib64' - I'm guessing that my python is 64 bit. I'm real new to python as I was wondering if someone could...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
13
by: James | last post by:
Hello, I'm a newbie to Python & wondering someone can help me with this... I have this code: -------------------------- #! /usr/bin/python import sys
30
by: Ivan Reborin | last post by:
Hello everyone, I was wondering if anyone here has a moment of time to help me with 2 things that have been bugging me. 1. Multi dimensional arrays - how do you load them in python For...
3
by: Ethan Furman | last post by:
len wrote: I've never had the (mis?)fortune to work with COBOL -- what are the files like? Fixed format, or something like a dBase III style? I
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.