Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old July 16th, 2003, 07:49 PM
RogueGeek RogueGeek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 RogueGeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old July 16th, 2003, 09:50 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #3  
Old July 16th, 2003, 10:15 PM
RogueGeek RogueGeek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 RogueGeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old July 17th, 2003, 08:31 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #5  
Old July 17th, 2003, 12:06 PM
inkedmn inkedmn is offline
Tattooed Python-Lovin' Freak-Boy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: orange county, CA
Posts: 16 inkedmn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to inkedmn Send a message via AIM to inkedmn
try importing string (though you should be able to use split without importing anything)

Reply With Quote
  #6  
Old July 17th, 2003, 02:24 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #7  
Old July 17th, 2003, 02:26 PM
RogueGeek RogueGeek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 RogueGeek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old July 17th, 2003, 02:52 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old July 17th, 2003, 05:32 PM
jimmy2k1 jimmy2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 89 jimmy2k1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 7
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:

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 *

Reply With Quote
  #10  
Old July 17th, 2003, 06:03 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > ? about "split" and strings


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |