Simple Text Processing

AJAskey askeyaj at
Thu Sep 10 18:06:46 CEST 2009

Never mind.  I guess I had been trying to make it more difficult than
it is.  As a note, I can work on something for 10 hours and not figure
it out.  But the second I post to a group, then I immediately figure
it out myself. Strange snake this Python...

Example for anyone else interested:

line = "this is a line"
print line
a = line.split()
print a
print a[0]
print a[1]
print a[2]
print a[3]


this is a line
['this', 'is', 'a', 'line']

On Sep 10, 11:36 am, AJAskey <aske... at> 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?
> Thanks.
> Andy

More information about the Python-list mailing list