strip/trim a string
Bjørn Ove Grøtan
bgrotanLOVELYSPAM at itea.ntnu.no
Mon Apr 22 07:22:11 EDT 2002
Martin Franklin wrote:
>
> On Monday 22 Apr 2002 9:32 am, Bjørn OveGrøtan wrote:
> > yes, I'm a newbie at python.
> >
> > and I need to strip/trim a string... e.g. I have a string of unknown
> > length, and only care
> > for the last five(5) characters.
> >
> > An example string:
> > xxxNOGBJ 94873
> >
> > where 94873 is the only part of the string I want to keep
>
> If it is allways the last five then :
>
> s="xxxNOGBJ 94873"
>
> s[-5:]
>
> at the python int.:
> $ python
> Python 2.0 (#11, Nov 6 2000, 09:01:06)
> [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
> Type "copyright", "credits" or "license" for more information.
> >>> s="xxxNOGBJ 94873"
> >>> s[-5]
> '9'
> >>> s[:-5]
> 'xxxNOGBJ '
> >>> s[-5:]
> '94873'
> >>>
Now this get a bit more complicated...
eksample-data I'm processing:
"76543";"4";"000X¤¤¤KOST998765;XXXX¤¤ORG
98765;XXXX¤NONSET876545;XXXX¤SIGNAL76543;";"Some information";"Even more
information";
This is excerpt from python-script:
---------------------------------------------------------
line = oufil.readline()
if not line:
break
fields = p.parse(line) # this parses the 5 main fields
hierarki = p.parse(fields[2]) # this parses the N fields in of
fields[2] which can be everything from 1 to 4 fields
if not fields:
continue
----------------------------------------------------------
What I'm struggeling for is this hash-table..
12345 Parent1-name
23456 Parent2-name, Parent1-name
34567 Parent3-name, Parent2-name, Parent3-name
45678 Parent4-name, Parent3-name, Parent2-name, Parent1-name
the csv-data is sorted before starting the script...
>From the hash-table I'm going to build ldap-files (ldif) for importing
into an ldap-server
typically:
dn= ou=Parent4-name, ou=Parent3-name, ou=Parent2-name,
ou=Parent1-name,dc=no
objectClass: top
objectClass: organizationalunit
ou: Parent4-name
(though I have to build ou's for those with no parent(parent1) first,
then parent2 and so on)
How do you suggest the design of such a system (even in pseudo-code)
----
BOG
More information about the Python-list
mailing list