* 'struct-like' list *
Schüle Daniel
uval at rz.uni-karlsruhe.de
Mon Feb 6 14:17:51 EST 2006
> I would like to have an array of "structs." Each struct has
>
> struct Person{
> string Name;
> int Age;
> int Birhtday;
> int SS;
> }
the easiest way would be
class Person:
pass
john = Person()
david = Person()
john.name = "John Brown"
john.age = 35
etc
think of john as namespace .. with attributes (we call them so) added on
runtime
better approch would be to make real class with constructor
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "person name = %s and age = %i" % (self.name, self.age)
john = Person("john brown", 35)
print john # this calls __str__
>
> I want to go through the file, filling up my list of structs.
>
> My problems are:
>
> 1. How to search for the keywords "Name:", "Age:", etc. in the file...
> 2. How to implement some organized "list of lists" for the data
this depend on the structure of the file
consider this format
New
Name: John
Age: 35
Id: 23242
New
Name: xxx
Age
Id: 43324
OtherInfo: foo
New
here you could read all as string and split it on "New"
here small example
>>> txt = "fooXbarXfoobar"
>>> txt.split("X")
['foo', 'bar', 'foobar']
>>>
in more complicated case I would use regexp but
I doubt this is neccessary in your case
Regards, Daniel
More information about the Python-list
mailing list