#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep 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.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep 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
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  8. #5
  9. No Profile Picture
    Tattooed Python-Lovin' Freak-Boy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    orange county, CA
    Posts
    16
    Rep Power
    0
    try importing string (though you should be able to use split without importing anything)
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    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.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep 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.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    89
    Rep Power
    13
    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:

    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 *
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    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.

IMN logo majestic logo threadwatch logo seochat tools logo