July 16th, 2003, 07:49 PM
? about "split" and strings
ok I'm new to python and this really has me stumped.
in the code below. It was working without having string.split or string.atoi Then after a bit of playing around with other stuff suddenly this part of the code started requiring the string.????
So I removed all of the extra stuff and made everything back to the original setup; and now it still requires the string.?????
What gives? why is python suddenly wanting me to specify string.?????? everytime
Any help would be appreciated.
yes it is spaced correctly in my code, the forum doesn't like me right now
def __init__(self, initial):
self.balance = initial
def deposit(self, amt):
self.balance = self.balance + amt
self.balance = self.balance - amt
fileerror = 0
acct = Account(0) #create an instance of ACCOUNT to 0
f = open("history.log","r") #load history file
# history file format = "16July2003104356|D|45.00|45.00"
lines = f.readlines(100000)
if not lines: #stops at the end of the file
for line in lines:
s = string.split (line,"|")
if s == "D":
elif s == "W":
print acct.getbalance() ," - - ", s # error checking
# Begin Main
Last edited by RogueGeek; July 16th, 2003 at 07:52 PM.
July 16th, 2003, 09:50 PM
Ok, in python 2+ there are several functions that you can apply to variable types (like a list and append). Most of what you can do with the string module you can now do without..
take a look at the built in functions in your python docs.
#split a string.
string_instance.split(char to split on)
#make the string upper case.
#make the string lower case.
#make the string title case.
#join a string.
Hope this clear's some things up for you,
July 16th, 2003, 10:15 PM
What has me flustered is this
split (line,"|") was working fine.
then without any reason I can find it quit working and started giving me this error.
NameError: global name 'split' is not defined
now the only way I can get this to work is to use
s = string.split (line,"|")
weird if you ask me.
it is also doing the same with atoi requires me to use string.atoi
July 17th, 2003, 08:31 AM
Sorry, all I was saying is that perhaps you used the built in string split instead of split from the string module. Anyway I can't see a good reason why it would work then stop working never had it happen to me. But if it works just use string, or use the built in, up to you.
July 17th, 2003, 12:06 PM
try importing string (though you should be able to use split without importing anything)
July 17th, 2003, 02:24 PM
Inkedmn, he did import string in his code above . The question was why did it work without importing string on min and then not the other. A question which I can't see an answer too other than that the interpriter was wierding out in some way, would need to know exactly what was done from start to finish to make any further analysis.
You can't use the string modules split function without importing string, you just get an error about an undefined function call. You can use .split(char) on a string instance/variable like i said above .
July 17th, 2003, 02:26 PM
I think my install of Python hates me
or maybe its just my PC that hates me.
It is starting to do weird stuff. hangs when you run a program.
Complains that my tabs are wrong, but the tabs are in the right spot.
I think I'm gonna try re-installing.
July 17th, 2003, 02:52 PM
Prob a good idea , what version do you have at the moment? Can I sugest 2.2.3, if you dont already have it it's the current stable release.
July 17th, 2003, 05:32 PM
that's cause all you did is:
in order to use the functions inside the string module without typing string.xxx, you have to import it like this
from string import *
July 17th, 2003, 06:03 PM
Dur why didn't i think of that . Of course if you imported all from string you wouldnt need the module prefix. But that doesn't really explain how it worked one min, then when he reverted back to the previous version it still didnt work unless you forgot to change the import statment back Rogue?
Anyway good thinking Jimmy , isn't it strange how you never knowtice what is right under your noes!