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

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?

Related

How to check for palindromes 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 1 year ago.
Improve this question
Can you write a method that checks whether a word or a phrase is a palindrome?
NOTE: Palindrome is a word which reads the same backwards. Eg = madam, racecar or phrases like "nurses run".
You can do it like this:
def palindrome(s):
s = s.replace(' ','') # This replaces all spaces ' ' with no space ''. (Fixes issues with strings that have spaces)
return s == s[::-1] # Check through slicing
palindrome('nurses run')

find the word before specific string 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
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]

How to remove all characters from a string after two white spaces 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
The string i needed to format is,
string = "How are you? abcdef"
I need to remove the "abcdef" from the string.
string = string.split(' ')[0]
Edit: Explanation. The line of code above will split the single string into a list of strings wherever there is a double space. It is important to note that whatever is split upon, will be removed. [0] then retrieves the first element in this newly formed list.

How to remove periods from the middle of sentences [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 6 years ago.
Improve this question
Is it possible to remove periods from the middle of a string (sentence), leaving the ending period?
The answers that I have seen, basically strip all of the periods.
Remove periods at the end of sentences in python
If I understand correctly, this should do what you want:
import re
string = 'You can. use this to .remove .extra dots.'
string = re.sub('\.(?!$)', '', string)
It uses regex to replace all dots, except if the dot is at the end of the string. (?!$) is a negative lookahead, so the regex looks for any dot not directly followed by $ (end of line).

Regex in python to catch a string like MMS-2839 [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 7 years ago.
Improve this question
How can I catch some string like these with a regex in python?
M1Sxs-2839
McS-28S9213
Both the first and the second part (divided by the -) can contains letters and numbers (case insensitive).
You may try the below re.match function.
re.match(r"(?i)[A-Z0-9]+-[A-Z0-9]+$", st)
(?i) helps to do case-insensitive match. Since re.match scans the input from start, you don't need to add start of the line anchor ^ explicitly.

Categories

Resources