473,508 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1000
* * 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
1291
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
3660
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
3309
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
6194
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
2675
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
2315
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
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.