0

I got an example.txt file, which contains:

4-1
9-3

as written format, string type. I need to open the file, and read lines, perform an operation and rewrite the file to:

4-1=3
9-3=6

But I constantly get an error when trying conversion, the problem I think is the - symbol, and it won't strip it, when I do f.readlines to get it as list there is ['4-1\n', '9-3\n']. I managed to strip \n symbol.

My code so far:

with open('./izrazi.txt', 'r+') as f:
    lst = f.readlines()
    print(lst)
    first = lst[0]
    first = first.strip('\n')
    print(first)
    print(first.isdigit())

I tried to convert to a set ,and remove -, after that I converted it back to list and there are 2 elements only but after every run of code they are changing index position, and so I cant subtract.

David Buck
  • 3,752
  • 35
  • 31
  • 35

3 Answers3

0

eval performs a maths operation on the line

with open('./izrazi.txt', 'r+') as f:
    lst = f.readlines()
    [f.write(i.strip('\n') + '=' + str(eval(i)) + '\n') for i in lst]

coderoftheday
  • 1,987
  • 4
  • 7
  • 21
0

I think.. something like this will do your job..

f = open('./izrazi.txt', 'r+')
text = f.readlines()
f.close()
f = open('./izrazi.txt', 'w+')
for line in text:
    res=eval(line)
    out=line.strip('\n') + '=' + str(res) + '\n'
    f.write(out)
f.close()
Johnson Francis
  • 249
  • 3
  • 17
0

eval() will definitely do the trick, but there is a school of thought that using eval() is best avoided, so I will present an alternative using regular expressions.

Trying to use Python functions like split() becomes tricky quite quickly as you can only split on a single character. Regular expressions would allow you to find the components of the equation more easily. For example this regular expression:

r'(\d+\.?\d*)([\-+/*])(\d+\.?\d*)'

specifically looks for 3 groups (within parentheses):
(\d+\.?\d*) looks for a number, with or without decimal place
([\-+/*]) looks for a mathematical symbol
so, together, they will find both numbers and any mathematical symbol you specify.

matches[0] contains the combination of all of the match groups, and matches[1], [2], [3] contain the first value, the operator, and the second value.

This enables you to perform the calculations without using eval()

import re

with open('./izrazi.txt', 'r+') as f:
    lst = f.readlines()
with open('./izrazi2.txt', 'w+') as f:
    for line in lst:
        matches = re.match(r'(\d+\.?\d*)([\-+/*])(\d+\.?\d*)', line)
        if matches:
            if matches[2] == "-":
                calc = float(matches[1]) - float(matches[3])
            if matches[2] == "+":
                calc = float(matches[1]) + float(matches[3])
            if matches[2] == "/":
                calc = float(matches[1]) / float(matches[3])
            if matches[2] == "*":
                calc = float(matches[1]) * float(matches[3])
            f.write(f"{matches[0]}={calc:g}\n")

With an input of:

4-2
9-3
3.142/22
34.2*91.44
9-3
3.142/22
34.2*91.4

This does give the desired output of:

4-2=2
9-3=6
3.142/22=0.142818
34.2*91.44=3127.25
9-3=6
3.142/22=0.142818
34.2*91.4=3125.88

As an aside, you can't use a set for operations such as - and / as they are not commutative. A set object is an unordered collection of distinct, hashable objects, so it simply not appropriate for use with mathematical terms.

David Buck
  • 3,752
  • 35
  • 31
  • 35