Python 初学者将字符串分成两个字符串
Python Beginner split String in two strings
我有一个 IP 范围 192.0.0.2-4
作为字符串,我想将它分成两个新的字符串
ip_start = '192.0.0.2'
ip_end = '192.0.0.4'
所以我必须在 "192.0.0.2-4"
中搜索 "-"
并在那里拆分,但是如何制作第二个字符串?
如果范围始终限于地址的最后一个字节(八位字节),则拆分地址的最后一个点并将其替换为您的结束值:
ip_start, _, end_value = iprange.partition('-')
first_three = ip_start.rpartition('.')[0]
ip_end = '{}.{}'.format(first_three, end_value)
我在这里使用了str.partition()
and str.rpartition()
,因为你只需要拆分一次;对于这种情况,这些方法要快一些。方法 return 3 个字符串,总是;分区字符串之前的所有内容、分区字符串本身以及之后的所有内容。因为我们只需要 .
分区的第一个字符串,所以我在那里使用索引 select 它进行赋值。
因为你不需要保留那个破折号或点,所以我将它们分配给了一个名为 _
的变量;这只是一个约定,用于表示您将完全忽略该值。
演示:
>>> iprange = '192.0.0.2-4'
>>> iprange.partition('-')
('192.0.0.2', '-', '4')
>>> iprange.partition('-')[0].rpartition('.')
('192.0.0', '.', '2')
>>> ip_start, _, end_value = iprange.partition('-')
>>> first_three = ip_start.rpartition('.')[0]
>>> ip_end = '{}.{}'.format(first_three, end_value)
>>> ip_start
'192.0.0.2'
>>> ip_end
'192.0.0.4'
为了完整起见:您也可以使用 str.rsplit()
method 从右侧拆分字符串,但在这种情况下您需要包含一个限制:
>>> first.rsplit('.', 1)
['192.0.0', '2']
此处的第二个参数 1
将拆分限制为找到的第一个 .
点。
您可以使用来自 "first" IP 地址的片段构建第二个字符串。
>>> def getIPsFromClassCRange(ip_range):
... # first split up the string like you did
... tmp = ip_range.split("-")
... # get the fix part of the IP address
... classC = tmp[0].rsplit(".", 1)[0]
... # append the ending IP address
... tmp[1] = "%s.%s" % (classC, tmp[1])
... # return start and end ip as a useful tuple
... return (tmp[0], tmp[1])
...
>>> getIPsFromClassCRange("192.0.0.2-4")
('192.0.0.2', '192.0.0.4')
这是 3 个步骤的解决方案,
使用 generator expression :
def ip_bounds(ip_string):
"""Splits ip address range like '192.0.0.2-4'
into two ip addresses : '192.0.0.2','192.0.0.4'"""
# split ip address with last '.'
ip_head, ip_tail = ip_string.rsplit('.', 1)
# split last part with '-'
ip_values = ip_tail.split('-')
# create an ip address for each value
return tuple('{}.{}'.format(ip_head,i) for i in ip_values)
ip_start, ip_end = ip_bounds('192.0.0.2-4')
assert ip_start == '192.0.0.2'
assert ip_end == '192.0.0.4'
我有一个 IP 范围 192.0.0.2-4
作为字符串,我想将它分成两个新的字符串
ip_start = '192.0.0.2'
ip_end = '192.0.0.4'
所以我必须在 "192.0.0.2-4"
中搜索 "-"
并在那里拆分,但是如何制作第二个字符串?
如果范围始终限于地址的最后一个字节(八位字节),则拆分地址的最后一个点并将其替换为您的结束值:
ip_start, _, end_value = iprange.partition('-')
first_three = ip_start.rpartition('.')[0]
ip_end = '{}.{}'.format(first_three, end_value)
我在这里使用了str.partition()
and str.rpartition()
,因为你只需要拆分一次;对于这种情况,这些方法要快一些。方法 return 3 个字符串,总是;分区字符串之前的所有内容、分区字符串本身以及之后的所有内容。因为我们只需要 .
分区的第一个字符串,所以我在那里使用索引 select 它进行赋值。
因为你不需要保留那个破折号或点,所以我将它们分配给了一个名为 _
的变量;这只是一个约定,用于表示您将完全忽略该值。
演示:
>>> iprange = '192.0.0.2-4'
>>> iprange.partition('-')
('192.0.0.2', '-', '4')
>>> iprange.partition('-')[0].rpartition('.')
('192.0.0', '.', '2')
>>> ip_start, _, end_value = iprange.partition('-')
>>> first_three = ip_start.rpartition('.')[0]
>>> ip_end = '{}.{}'.format(first_three, end_value)
>>> ip_start
'192.0.0.2'
>>> ip_end
'192.0.0.4'
为了完整起见:您也可以使用 str.rsplit()
method 从右侧拆分字符串,但在这种情况下您需要包含一个限制:
>>> first.rsplit('.', 1)
['192.0.0', '2']
此处的第二个参数 1
将拆分限制为找到的第一个 .
点。
您可以使用来自 "first" IP 地址的片段构建第二个字符串。
>>> def getIPsFromClassCRange(ip_range):
... # first split up the string like you did
... tmp = ip_range.split("-")
... # get the fix part of the IP address
... classC = tmp[0].rsplit(".", 1)[0]
... # append the ending IP address
... tmp[1] = "%s.%s" % (classC, tmp[1])
... # return start and end ip as a useful tuple
... return (tmp[0], tmp[1])
...
>>> getIPsFromClassCRange("192.0.0.2-4")
('192.0.0.2', '192.0.0.4')
这是 3 个步骤的解决方案, 使用 generator expression :
def ip_bounds(ip_string):
"""Splits ip address range like '192.0.0.2-4'
into two ip addresses : '192.0.0.2','192.0.0.4'"""
# split ip address with last '.'
ip_head, ip_tail = ip_string.rsplit('.', 1)
# split last part with '-'
ip_values = ip_tail.split('-')
# create an ip address for each value
return tuple('{}.{}'.format(ip_head,i) for i in ip_values)
ip_start, ip_end = ip_bounds('192.0.0.2-4')
assert ip_start == '192.0.0.2'
assert ip_end == '192.0.0.4'