Parsing text

Michael Hudson mwh21 at cam.ac.uk
Sun Nov 28 14:34:53 EST 1999


"Troy" <trekkan at yahoo.com> writes:

> As you will be able to tell, I am new to Python so please forgive any newbie
> things I might do. =)

Welcome!

> What I would like to do is take a variable and parse out each of the
> pieces.  Most commonly delimited by spaces.  Example text:
> 
> This is a test
> 
> I would like to be able to pull out any of "This", "is", "a", "test"
> and be able to drop any element into a variable that I wanted.

string.split?

I.e. 

Python 1.5.2+ (#12, Nov 19 1999, 15:00:31)  [GCC 2.95.2 19991024 (release)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import string
>>> string.split("This is a test")
['This', 'is', 'a', 'test']

> I hope this explains it enough.  Thanks for any help you may be able
> to provide.
> 
> One other thing, is there a way to tell if a substring exists in a
> string?  in a basic sort of way
> 
> if 'abc' isin '123abc456'

string.find?

>>> string.find("1122abc33","abc")
4
>>> string.find("1122abc33","f")
-1

> Something to that effect anyway.

In general

http://www.python.org/doc/current/lib/module-string.html

and maybe

http://www.python.org/doc/current/tut/tut.html

?

> Thanks for your time!

No problem!

Cheers,
Michael




More information about the Python-list mailing list