Wayne Watson wrote:
Thanks. I had a hunch that might be a good way to do it. I saw
something like this in other s/w. Now I know what they were up to.
Omer wrote:
Class Person:
def __init__(str):
self.Firstname = str[0:4]
self.Surname = str[5:7]
(...)
If your string separates the values of each person using tags rather
than fixed lengthes, build it like:
or:
self.Firstname = str[0:str.find("Last name:")]
self.Surname = str[str.find("Last name:")+len("Last
name:"):str.find("date_of_birth")]
And just create a list of these, adding various get methods for easy
information retrieval.
(Here's for wandering whether my way of doing it counts as Pythonic.)
HTH.
Omer.
On Thu, Sep 11, 2008 at 4:58 PM, Wayne
Watson <[EMAIL PROTECTED]>
wrote:
True enough, but that gets
messy. I'd have to keep them perhaps as
global variables or pass then around a lot from function to function as
a collection. I see WW posted above you about dictionaries. Maybe
that's the way to do it. I'll look into it.
Kent Johnson wrote:
On Thu, Sep 11, 2008 at 6:58 AM, Wayne Watson
<[EMAIL PROTECTED]> wrote:
Is it possible in Python to look at a string as a "struct". I don't think a
struct exists in python. Actually, is there something analogous to a record.
In the case of strings, suppose I have string that is composed of
sub-strings like, first_name, last-name, date_of birth, which consists of
month, day, and year, and finally SSN, street_address, state, city, and
zip_code. I'd like to access these fields directly instead of lastname =
record[38:55]. What if fields are not just strings, but some numeric values?
For numeric fields, just convert as needed:
quantity = int(record[55:60])
price = float(record[60::70])
If the numbers are binary, rather than ascii, see the struct module.
Kent
--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
"If voting made any difference they wouldn't let us do it."
-- Mark Twain
Web Page: <www.speckledwithstars.net/>
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor
--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
"If voting made any difference they wouldn't let us do it."
-- Mark Twain
Web Page: <www.speckledwithstars.net/>
--
Signature.html
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
"If voting made any difference they wouldn't let us do it."
-- Mark Twain
Web Page: <www.speckledwithstars.net/>
|