|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
|||
|
|||
|
? 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. |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
try importing string (though you should be able to use split without importing anything)
|
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
||||
|
||||
|
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. |
|
#9
|
|||
|
|||
|
Quote:
that's cause all you did is: Code:
import string 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 * |
|
#10
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > ? about "split" and strings |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|