Text manipulation to form an equation

2024/7/4 15:58:26

a=0.77 ,b=0.2 ,c=0.20, d=0.79 ,z=(c+d), e=(z*a) ,output=(z+e)

I have a text file like above. I need a parser logic that will throw an equation like output=(0.20+0.79)+((0.20+0.79)*a) what are some efficient ways to do it? Are there any libraries? Thank you!

Answer

Primitive method is to work with strings and use replace()


First use split(',') to convert string to list

['a=0.77 ', 'b=0.2 ', 'c=0.20', ' d=0.79 ', 'z=(c+d)', ' e=(z*a) ', 'output=(z+e)']

Next use .strip() to remove spaces from ends and begins.

Next use .split('=') on every element to create nested lists.

[['a', '0.77'], ['b', '0.2'], ['c', '0.20'], ['d', '0.79'], ['z', '(c+d)'], ['e', '(z*a)'], ['output', '(z+e)']]

Next use dict() to create dictionary.

{'a': '0.77','b': '0.2','c': '0.20','d': '0.79','e': '(z*a)','output': '(z+e)','z': '(c+d)'}

And now you can get first 'a' : '0.77 to run .replace('a', '0.77)` on other items in dictionary. And repeate it with other values from dictionary.

So finally you could get dictionary

{'a': '0.77','b': '0.2','c': '0.20','d': '0.79','e': '((0.20+0.79)*0.77)','output': '((0.20+0.79)+((0.20+0.79)*0.77))','z': '(0.20+0.79)'}

and output has string ((0.20+0.79)+((0.20+0.79)*0.77))


import sympy
import pprinttext = 'a=0.77 ,b=0.2 ,c=0.20, d=0.79 ,z=(c+d), e=(z*a) ,output=(z+e)'parts = text.split(',')                     # create list
#print(parts)
parts = [item.strip() for item in parts]    # remove spaces
#print(parts)
parts = [item.split('=') for item in parts] # create tuples
#print(parts)
parts = dict(parts)                         # create dict
#print(parts)pprint.pprint(parts)for key1, val1 in parts.items():for key2, val2 in parts.items():parts[key2] = parts[key2].replace(key1, val1)pprint.pprint(parts)print('output:', parts['output'])
https://en.xdnf.cn/q/120047.html

Related Q&A

Python If statement and logical operator issue [duplicate]

This question already has answers here:How to test multiple variables for equality against a single value?(31 answers)Closed 6 years ago.Im trying to create a small python program to emulate rolling a…

How do I replace a specific string in a 2d array?

Im making a program that Identifies if a blank tile exists or not. I already have a code in my 2d array which is arr2 = [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0][0 0 0 0 0 0 0 0 0 0 0 0 0 0 0][0 0 0 0 0 0 0 0 0…

from _dlib_pybind11 import * ModuleNotFoundError: No module named _dlib_pybind11

I actually working on a face recognition project but getting an error such as: from _dlib_pybind11 import * ModuleNotFoundError: No module named _dlib_pybind11Please help Ill appreciate any bits of hel…

Sudoku with user input

I am trying to write a Sudoku game with user input. So the user can choose what row/column it wants to and what number. I have to import it from a text file and have made a save and load function. I ha…

Move user to a textchannel as soon as he has pressed a button

I have a problem. I would like to move a user from one specific textchannel to another specific textchannel as soon as he has pressed a button. Unfortunately I get an error. class MyView(discord.ui.Vie…

scrape the about page of websites with Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Tkinter entry widget execution [duplicate]

This question already exists:Entry widget in tkinterClosed 2 years ago.So I made a simple program however it doesnt seem to work my code is: e = Entry(root, font = 20,borderwidth=5)e.grid(row=1)def cap…

Python Redefine the variable given as a parameter to the function

Hello I am trying to make theme window with tkinter. There is 5 variable for different widgetss color. I will use color dialog for choosing colors but I dont want to define 5 functions. So I think I ca…

Need a Gui Keypad for a touchscreen that outputs a pin when code is correct

I have a raspberry pi with a touchscreen running raspbian, Im hoping to have a Gui on the touchscreen that had a number keypad that when a correct input is entered a pin will output to a door latch or …

How can i make the python to wait till i complete speaking?

I am writing a program to recognise the speech from a microphone and the code will process accordingly. The code I wrote for this purpose is below.import speech_recognition as sr import webbrowser impo…