find the word before specific string in python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have string
exp='"\"OLS\".\"ORDER_ITEMS\".\"QUANTITY\" <50 and \"OLS\".\"PRODUCTS\".\"PRODUCT_NAME\" = 'Kingston''
How can I find word before string \"OLS\".\"PRODUCTS\".\"PRODUCT_NAME\" = 'Kingston' that is and in my case
result should be and

Here's one way (after fixing your string literal):
import re
exp='"OLS"."ORDER_ITEMS"."QUANTITY" <50 and "OLS"."PRODUCTS"."PRODUCT_NAME" = \'Kingston\''
search_for = '"OLS"."PRODUCTS"."PRODUCT_NAME"'
m = re.search(r'(\w+)\s+' + search_for, exp)
print(m.group(1))
Result:
and
Note that there's no reason to escape the double quote characters in your string, since you're defining the string with single quotes. For the same reason, you do have to escape the single quotes around Kingston.

This?
key = "\"OLS\".\"PRODUCTS\".\"PRODUCT_NAME\" = 'Kingston'"
exp[:exp.index(key)].split()[-1]

Related

replace all int's in string with _ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
How can I separate data types in a string or list so they can be set to another character, I assume there is something I have missed but everything i have tried so far has now worked for me. so far have tried so split into list and use a for loop to find every int but I can't find a way to differentiate the data types so it can change every int.
You can use regex :
import re
re.sub("\d", "_", "10 4 2")
\d matches any decimal digit character.

How to keep the only characters I want [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to keep a certain character for my python project which and I don’t want to replace every unused character with ‘’ is there any way to do it?
You can use a function re.sub in re library
For example:
data = re.sub('0123456789abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNNM+/-', '', data)
This will keep every character in the first parameter replace with the second parameter by using String from the third parameter
if you want to keep "d" in the string
origin = "abcdefghidx"
result = "".join([c for c in origin if c=="d"])
You can use str.replace('what to want to delete', 'what you want to add').
ex-
name = "stackoverflow"
newName = name.replace('o', '0')
newName becomes 'stack0verfl0w'

Extract Data Enclosed between three asterisks in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want the data enclosed between three asterisks.And the Word should start with description.
For eg:I have data like
description ***tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568***;
I want only
tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568
You may use re.findall here:
inp = "description ***tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568***;"
matches = re.findall(r'\bdescription\s+\*{3}(.*?)\*{3}', inp, flags=re.DOTALL)
print(matches)
This prints:
['tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568']
Note that I use dot all mode in the regex, in case your expected matches might span across more than one line.

Remove words in string if it contains \u in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to remove words in string if it contains \u in python?
ex:
string ="\uf064thickness cfoutside\uf0d7\uf03a\uf03d TC2019 45TRCMat"
The final output should be like this.
"TC2019 45TRCMat"
After removing all of the words if it contains \u.
Rather then looking to remove unicode character go the other way and only allow ascii character:
string ="\uf064thickness cfoutside\uf0d7\uf03a\uf03d TC2019 45TRCMat"
def is_ascii(s):
return all(ord(c) < 128 for c in s)
for s in string.split(" "):
if is_ascii(s):
print(s)
Reference: How to check if a string in Python is in ASCII?

How to remove single quotes in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have
filename = src_filename
print filename
somehow it is printing filename = 'apple' instead of just apple. Is there a way I can remove these single quotes.
The single quotes denote a string literal
>>> 'apple'
'apple'
If you print the string, it will not use the single quotes
>>> print('apple')
apple
So in your case you can simply
print(filename)
>>> print(filename.strip('"\''))

Categories

Resources