[Tutor] A question

Marilyn Davis marilyn at deliberate.com
Sun Feb 5 15:21:36 CET 2006


On Sun, 5 Feb 2006, Marilyn Davis wrote:

> On Sun, 5 Feb 2006, Bian Alex wrote:
> 
> > How can I get a string from a random file.
> > For Example:
> > Del    ete On   :   Copy
> > Owner   :  Bn
> > Personalized: 5
> > PersonalizedName        :          My    Documents
> > 
> > I want to get the string after 'Owner' ( In example is 'Bn')
> > 
> > 'import re' ?
> 
> Yes, but maybe it's simpler to use the builtin functions, and slicing:
> 
> text = open('the_file').readlines()  <-- now the_file is in text as a string

Ooops.  This should be:

text = open('the_file').read()

Maybe someone else will show you the readlines() solution.  I have to
run.

Good luck.

Marilyn

> 
> start = text.find('Owner   :') + 8   <-- start is the index into the text
>                                          where Owner starts + 8 to point
>                                          to the text you want.
> end = text.find('\n', start)         <-- end is where the newline is, after
>                                          start.
> found = text[start:end].strip()      <-- found has the text you want, with
>                                          any spaces stripped off.
> 
> I hope it helps.  I didn't test it.
> 
> Marilyn Davis
> 
> > 
> > Pls help.
> > 
> 
> 

-- 



More information about the Tutor mailing list