Simple Text Processing

Benjamin Kaplan benjamin.kaplan at case.edu
Thu Sep 10 11:45:27 EDT 2009


On Thu, Sep 10, 2009 at 11:36 AM, AJAskey <askeyaj at gmail.com> wrote:

> New to Python.  I can solve the problem in perl by using "split()" to
> an array.  Can't figure it out in Python.
>
> I'm reading variable lines of text.  I want to use the first number I
> find.  The problem is the lines are variable.
>
> Input example:
>  this is a number: 1
>  here are some numbers 1 2 3 4
>
> In both lines I am only interested in the "1".  I can't figure out how
> to use "split()" as it appears to make me know how many space
> separated "words" are in the line.  I do not know this.
>
> I use:  a,b,c,e = split() to get the first line in the example.  The
> second line causes a runtime exception.  Can I use "split" for this?
> Is there another simple way to break the "words" into an array that I
> can loop over?
>
> >>> line = "here are some numbers 1 2 3 4"
>>> a = line.split()
>>> a
['here', 'are', 'some', 'numbers', '1', '2', '3', '4']
>>> #Python 3 only
... a,b,c,d,*e = line.split()
>>> e
['1', '2', '3', '4']




> Thanks.
> Andy
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090910/09400848/attachment-0001.html>


More information about the Python-list mailing list