How do I select random text on screen [closed]

2024/7/7 5:57:21

How do I select the random text that is after a fixed text using Python? For Example "AWB NO 56454546" where "AWB NO" is fixed text while "56454546" is random text.

Answer

You can use the partition method for this. It's part of the built-in str type.

>>> help(str.partition)partition(self, sep, /)Partition the string into three parts using the given separator.This will search for the separator in the string.  If the separator is found,returns a 3-tuple containing the part before the separator, the separatoritself, and the part after it.If the separator is not found, returns a 3-tuple containing the original stringand two empty strings.

If you use "AWB No: " as the separator, you'll get back a 3-tuple containing:

  • everything before "AWB No: " e.g. "Courier "
  • the separator: "AWB No: "
  • everything after "AWB No: ": "56454546"

So you can get that "everything after" part in two ways:

input_str = "Courier AWB No: 56454546"
sep = "AWB No: "
before, sep, after = input_str.partition(sep)
# == "Courier ", "AWB No: ", "56454546"# or
after = input_str.partition(sep)[2]# either way: after == "56454546"

If there are more words after the number you can get rid of them with .split()[0]:

input_str = "Courier AWB No: 56454546 correct horse battery staple"
sep = "AWB No: "
after = input_str.partition(sep)[2]
awb_no = after.split()[0]# after == "56454546"

Or in one line:

input_str = "Courier AWB No: 56454546 correct horse battery staple"
awb_no = input_str.partition("AWB No: ")[2].split()[0]
https://en.xdnf.cn/q/120633.html

Related Q&A

Conditional return with no else [duplicate]

This question already has answers here:Python Ternary Operator Without else(9 answers)Closed 10 months ago.Im try to do something like that in Python:return None if a is NoneInstead of having:if a is N…

Finding the position of an item in another list from a number in a different list

Developing on my previous question Im wondering whether there is a way to have an element in a list and find that position in another list and return what is in the position as a variable. for example:…

Why am I getting array instead of vector size? [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 3 years ago.Improve…

Sending Email attachment (.txt file) using Python 2.7 (smtplib) [duplicate]

This question already has answers here:How to send email attachments?(21 answers)Closed 9 years ago.So Im trying to send a .txt file as an attachment and I cant find the right code to work. Here is my…

Python selenium drop down menu click

i want to select option from a drop down menu, for this i use that :br.find_element_by_xpath("//*[@id=adyen-encrypted-form]/fieldset/div[3]/div[2]/div/div/div/div/div[2]/div/ul/li[5]/span").c…

TypeError: Argument must be rect style object - Pygame (Python [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Pygame (Python) - TypeError: Argument must be rect style object I am trying to make a brick breaker game in Pygame (with P…

How to compare dates in python and find the greater one

I want to compare 2 date and predict a label true if date 1 greater than date 2 and predict false date 1 less than date 2. I have trained the model but model is predicting wrong for near by dates that …

Python: is isalnum() the same as isalpha with isdigit?

Is there a way to concretely verify this? I tried to solve a coding question but it seems one of the test cases (not revealed to me) takes this as wrong. In what kinds of cases does this fail to be tr…

Python code works fine first time, but fails second time

The first time I run this block of code from Notebook it works fine:#Which letters and how many letters = ["a","b","c"] noOfLetters = len(letters)#Looking for all permutat…

How do I subtract a value in the dictionary? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…