The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
? about "split" and strings
Discuss ? about "split" and strings in the Python Programming forum on Dev Shed. ? about "split" and strings Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 16th, 2003, 07:49 PM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
? 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.
Brian
---------------------------------------------------------------------
Code:
import string
class Account:
def __init__(self, initial):
self.balance = initial
def deposit(self, amt):
self.balance = self.balance + amt
def withdraw(self,amt):
self.balance = self.balance - amt
def getbalance(self):
return self.balance
def startup():
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"
while 1:
lines = f.readlines(100000)
if not lines: #stops at the end of the file
break
for line in lines:
s = string.split (line,"|")
if s[1] == "D":
acct.deposit(string.atoi(s[2]))
elif s[1] == "W":
acct.withdraw(string.atoi(s[2]))
print acct.getbalance() ," - - ", s[3] # error checking
# Begin Main
startup()
yes it is spaced correctly in my code, the forum doesn't like me right now 
Last edited by RogueGeek : July 16th, 2003 at 07:52 PM.
|

July 16th, 2003, 09:50 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
built in
 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..
Code:
#split a string.
string_instance.split(char to split on)
#make the string upper case.
string_instance.upper()
#make the string lower case.
string_instance.lower()
#make the string title case.
string_instance.title()
#join a string.
string_to_join_with.join(list)
take a look at the built in functions in your python docs.
Hope this clear's some things up for you,
have fun,
Mark.
|

July 16th, 2003, 10:15 PM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
not really.
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
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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.
Take care,
Mark.
|

July 17th, 2003, 12:06 PM
|
|
Tattooed Python-Lovin' Freak-Boy
|
|
Join Date: Dec 2001
Location: orange county, CA
Posts: 16
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
try importing string (though you should be able to use split without importing anything)
|

July 17th, 2003, 02:24 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
ok?
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  .
Take care,
Mark.
|

July 17th, 2003, 02:26 PM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
Have fun,
Mark.
|

July 17th, 2003, 05:32 PM
|
|
Contributing User
|
|
Join Date: Mar 2002
Posts: 89
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 12
|
|
Quote: Originally posted by RogueGeek
not really.
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 |
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
Code:
from string import *
|

July 17th, 2003, 06:03 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
DUR!!!
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!
Take care,
Mark.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|