我如何在屏幕上 select 随机文本
How do I select random text on screen
如何使用 Python select 固定文本后的随机文本?例如“AWB NO 56454546”,其中“AWB NO”是固定文本,而“56454546”是随机文本。
您可以使用 partition
方法。它是 built-in str
类型的一部分。
>>> 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 separator
itself, and the part after it.
If the separator is not found, returns a 3-tuple containing the original string
and two empty strings.
如果您使用 "AWB No: "
作为分隔符,您将得到一个三元组,其中包含:
"AWB No: "
之前的所有内容,例如"Courier "
- 分隔符:
"AWB No: "
"AWB No: "
之后的所有内容:"56454546"
所以您可以通过两种方式获得“之后的所有内容”部分:
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"
如果数字后面还有更多的单词,你可以用 .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"
或一行:
input_str = "Courier AWB No: 56454546 correct horse battery staple"
awb_no = input_str.partition("AWB No: ")[2].split()[0]
如何使用 Python select 固定文本后的随机文本?例如“AWB NO 56454546”,其中“AWB NO”是固定文本,而“56454546”是随机文本。
您可以使用 partition
方法。它是 built-in str
类型的一部分。
>>> 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 separator
itself, and the part after it.
If the separator is not found, returns a 3-tuple containing the original string
and two empty strings.
如果您使用 "AWB No: "
作为分隔符,您将得到一个三元组,其中包含:
"AWB No: "
之前的所有内容,例如"Courier "
- 分隔符:
"AWB No: "
"AWB No: "
之后的所有内容:"56454546"
所以您可以通过两种方式获得“之后的所有内容”部分:
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"
如果数字后面还有更多的单词,你可以用 .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"
或一行:
input_str = "Courier AWB No: 56454546 correct horse battery staple"
awb_no = input_str.partition("AWB No: ")[2].split()[0]