Is there an easy way to do this?

wickedDUDE

New member
Jun 25, 2006
1,054
12
0
Hello,

I have a file that has hundreds of integers and + symbols in it.

i.e. 45+14, 12+35, 98+12, etc.

As shown above, the numbers and + symbols are together, but I simply want the total to be shown instead of the two numbers.

One thing that makes it a little more difficult is that some of the numbers have a period "." or a bracket "(" to the right of the second digit (i.e. 98+12.01 or 98+12(... but I only want to add the 98+12 together.

To make things clear, I would like:

98+12.01 to be replaced by 110.01, and so on.

Any help is appreciated. This needs to be done in a big file, so it would be great if I could automate it some how.
 


Well, it really depends on the file. I could write something based on your example, but its probably not accurate. Eg your sample suggests the following:

All sets are separated by a comma and a space
The 'weird' characters are always after the 2nd integer
All integers are 2 figures.

If this was true for the whole file, its pretty simple.
 
Well, it really depends on the file. I could write something based on your example, but its probably not accurate. Eg your sample suggests the following:

All sets are separated by a comma and a space
The 'weird' characters are always after the 2nd integer
All integers are 2 figures.

If this was true for the whole file, its pretty simple.

Congratulations, you made it complicated.
 
Isn't there an easy way to do this in Excel? I need to do it asap....

I can replace the numbers with =SUM(98+1) and then drop it into Excel, but the only problem is the file is a bit messy with other text and brackets, so Excel doesn't recognize the formula and it remains unchanged.
 
you could delimit the data into seperate columns, do the sums, then concatenate it back together however you want.
 
Thanks for the replies.

I may have narrowed it down, although still need some help:

If I copy my entire file into Excel, I now have many instances of:

=SUM(x+x)

The problem is that I have text that precedes this formula in the cell, so Excel isn't recognizing the formula is there. Is there a way to somehow force it to calculate any instance of =SUM(x+x) , regardless of where it is in the data cell and what comes before or after it?
 
I would've used Python. First, string.replace('[brackets and dots and bs], ''), then split the string into an array, with each [N]+[N] as an array item. Then loop through the array, taking each item and splitting on the '+', converting to integers and summing together and saving the sum.

Should take like 5 minutes, even if you don't know Python.

There might be a few more steps to clean up your file, like if you have extra spaces and crap to get rid of, but still pretty straightforward and can be done quickly.
 
This probably isn't 100% correct, but it should get you close, I just typed it in off the top of my head, so there could be some errors. Of course, you need to adapt it to work with your input file.

Code:
filename = open('yourfile.txt', 'r')
contents = filename.read()
filename.close()
contents = contents.replace('.', '')
# .... etc.

pairs = contents.split()
# assuming pairs are separated by spaces after data sanitization above

sums = []
for pair in pairs:
    values = pair.split('+')
    sum = int(values[0]) + int(values[1])
    sums.append(sum)


sums_content = ""
for sum in sums:
    sums_content += str(sum) + "\n"

filename = open('sums.txt', 'w')
filename.write(sums_content)
filename.close()
 
wickedDUDE. Use a function in excel called "text to columns". Use a custom delimiter + in this case. That will split the contents with the + so now you should have 2 columns of numbers instead of one.

Then add another column which does a sum of col1 + col2 and you got the value your after.

EDIT to remove the ('s just do a simple replace. Make sure you select just the column you want it to process and ensure that in the replace settings it's setup correctly.
 
You could also just clean the file up in any decent text editor. Would be pretty quick work with Emacs macros, I'm sure whatever you use has similar functionality (search and replace, etc.)
 
Post a sample from the file, 10 different people would have fixed this by now if they actually saw the file and not a vague description.
 
Thanks all. Here's a better description:

I have lots of cases of the following in the document:

m="12+28.00x"

I just want to add all numbers before the + sign with the number immediately after the + sign, but before the period. That's all.

In this case the result would be m="40.00x"
 
------

post sample, or give up. I know what you want to do, i know what some of the data looks like, i cant help because i dont know if you are omitting something important, and you dont know if your omitting something important either, so post the damn file.