471,119 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

Processing and adding numbers from lines.

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

Jan 5 '06 #1
1 1206
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

Jan 5 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Daniel Rudy | last post: by
6 posts views Thread by Hareth | last post: by
6 posts views Thread by sanfranc415 | last post: by
17 posts views Thread by Sri | last post: by
16 posts views Thread by malibuster | last post: by
60 posts views Thread by Bill Cunningham | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.