Simple Text Processing

A

AJAskey

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
 
A

AJAskey

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]

------
OUTPUT:

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

Steven D'Aprano

Basically, when you're good with Perl, you start to think of every task
in terms of regular expression matches. When you're good with Python,
you start to think of every task in terms of lists and tuples.

Not me -- I think of most such tasks in terms of iterators.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top