Processing and adding numbers from lines.

S

sanfranc415

Hello, Group:

Is there an easy way to automate the processing of adding the following
numbers in two lines?

test test test description (100-10-0, 6700 test)
test test test description (100-10-0, 6350 test)

I'd like to be able run a script on the file above to produce the
following results.

200-20 .9090

The lines will stay the same, but the numbers will change.

--SF
 
B

bearophileHUGS

With your input this returns:
[200, 20, 0, 13050]
so it's not what you want, but maybe it can be a starting point for
you:

from re import findall
txt = """\
test test test description (100-10-0, 6700 test)
test test test description (100-10-0, 6350 test)"""
lines = txt.split("\n")
res = [map(int, findall("[0-9]+", line)) for line in lines]
print map(sum, zip(*res))

Maybe izip and imap can be useful too.
lines can be a file object too.
Bye,
bearophile
 

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

Forum statistics

Threads
474,275
Messages
2,571,381
Members
48,070
Latest member
nick_tyson

Latest Threads

Top