This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 4 years ago.
I mean I have this string var:
mystr1 = "1==1 or 1==2"
mystr2 = "1==1 and 1==2"
if_logical_string(mystr1) must be True
if_logical_string(mystr2) must be False
How can I achieve this? Is there any lib to do so ?
Thanks.
mystr1 = "1==1 or 1==2"
mystr2 = "1==1 and 1==2"
# will output True
print(eval(mystr1))
# will output False
print(eval(mystr2))
If you have a mathematical expression, there is a more elegant way using Pyparsing. Check out this post: from Stackoverflow
Yes, you can use python's eval function.
However, I would recommend having another approach... There's always another solution...
Related
This question already has answers here:
Pythonic way to replace chars
(2 answers)
Closed 1 year ago.
I have a problem when trying to switch the signs of all arithmetic operations inside a string in Python.
My input looks like this: "+1-2"
And the output should be something like that: "-1+2"
But when I try to replace the characters with replace() function:
"+1-2".replace("+", "-").replace("-", "+")
The output I get is that: '+1+2'
Looks like the replace functions are replacing everything at the same time, so it will never switch it correctly in that way. A one-liner solution with similar functions would be very helpful.
Use str.translate:
s = "+1-2+3+4-2-1"
t = str.maketrans('+-','-+')
print(s.translate(t))
Output:
-1+2-3-4+2+1
This question already has an answer here:
Python: How to use re.search() in a compound if statement (is it even possible?)
(1 answer)
Closed 2 years ago.
While parsing lines I am using regex. I assign the re.search to a variable and then perform a conditional check on it.
x=re.search(....)
if x:
Is there a way to do the search and if condition in the same line?
Thanks!
if (re.search(...)):
Doesn't this option work?
If you need to work with x further, use :=
This question already has answers here:
Python- how do I use re to match a whole string [duplicate]
(4 answers)
Closed 6 years ago.
I have two variables storing path in python
var1 = u'Manish/testfolder/.*txt'
var2 = u'Manish/testfolder/test.txt.abc'
if I type
re.match(var1 , var2 )
it yeilds true can someone explain how is it evaluating to true and what I shall I do so that it returns false.
Your regex is included inside var2.
If you explicit set an end for your regex, it's not matching anymore:
var1 = u'Manish/testfolder/.*txt$'
As mentioned here and here re.match() checks for a match only at the beginning of the string.
This question already has answers here:
Search and replace operation
(2 answers)
Closed 6 years ago.
I have written code to search and replace string in make command as per user input
make language='english' id=234 version=V1 path='/bin'
In above code i searched version=V1 and replace version with version=V2
import re
strings = "make language='english' id=234 version=V1 path='/bin'"
search_pattern= re.search('version=(.*?)\s', strings)
old_str = search_pattern.group(1)
print test.replace(old_str, "V2")
Can anyone help me write above code in pythonic way or any other way to write above code
It's very easy if you use str.replace
String = "make language='english' id=234 version=V1 path='/bin'"
String = String.replace("version=V1", "version=V2")
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 6 years ago.
It's really odd but I have this string:
"['please', 'help']"
I want something that would get one argument at a time.
I've searched everywhere for this but I didn't find anything.
Thanks in advance
While eval is a correct approach, it can often have negative consequences. I suggest using ast.literal_eval, which is a more safe approach (as mentioned by the linked docs):
import ast
s = "['please', 'help']"
s_list = ast.literal_eval(s)
print s_list
Are you looking for something like this?
string = "['please', 'help']"
string_list = eval(string)
print string_list[0], string_list[1]
Edit: you should ideally use ast.literal_eval as the other answer suggests, if you are unsure of what the string contains.