Python如何在分隔符不存在时处理拆分?

时间:2022-09-23 07:46:46

I have the following python code:

我有以下python代码:

def split_arg(argv):
    buildDescriptor = argv[1]
    buildfile, target = buildDescriptor.split("#")

    return buildfile, target

It expects a string (argv[1]) of the form buildfile#target and splits them into two variables of the same name. So a string like "my-buildfile#some-target" will get broken into my-buildfile and some-target respectively.

它需要一个构造buildfile #target的字符串(argv [1])并将它们分成两个同名的变量。所以像“my-buildfile#some-target”这样的字符串将分别分解为my-buildfile和some-target。

Sometimes though, there won't be "#" and target; sometimes you'll just have "my-buildfile", in which case I just want target to be "" (empty).

有时候,不会有“#”和目标;有时你只会有“my-buildfile”,在这种情况下我只想让目标成为“”(空)。

How do I modify this function so that it will handle instances where "#" doesn't exist and it returns buildfile with an empty target?

如何修改此函数以便它将处理“#”不存在的实例并返回带有空目标的buildfile?

Currently, if I pass just the buildfile, it throws an error:

目前,如果我只传递构建文件,它会抛出一个错误:

buildfile, target = buildDescriptor.split("#")
ValueError: need more than 1 value to unpack

Thanks in advance!

提前致谢!

3 个解决方案

#1


6  

First, put the result of the split in a list:

首先,将拆分结果放在一个列表中:

split_build_descriptor = buildDescriptor.split("#")

Then check how many elements it has:

然后检查它有多少元素:

if len(split_build_descriptor) == 1:
    buildfile = split_build_descriptor[0]
    target = ''
elif len(split_build_descriptor) == 2:
    buildfile, target = split_build_descriptor
else:
    pass  # handle error; there's two #s

#2


8  

I'd use the obvious approach:

我会使用明显的方法:

    buildfile, target = buildDescriptor.split("#") if \
                        "#" in buildDescriptor else \
                        (buildDescriptor, "")

Note that this will also throw an Exception when there is more than one "#" in buildDescriptor (which is generally a GOOD thing!)

请注意,当buildDescriptor中存在多个“#”时,这也会抛出异常(这通常是一件好事!)

#3


2  

>>> buildfile, _, target = "hello#world".partition("#")
>>> buildfile, target
('hello', 'world')
>>> buildfile, _, target = "hello".partition("#")
>>> buildfile, target
('hello', '')

#1


6  

First, put the result of the split in a list:

首先,将拆分结果放在一个列表中:

split_build_descriptor = buildDescriptor.split("#")

Then check how many elements it has:

然后检查它有多少元素:

if len(split_build_descriptor) == 1:
    buildfile = split_build_descriptor[0]
    target = ''
elif len(split_build_descriptor) == 2:
    buildfile, target = split_build_descriptor
else:
    pass  # handle error; there's two #s

#2


8  

I'd use the obvious approach:

我会使用明显的方法:

    buildfile, target = buildDescriptor.split("#") if \
                        "#" in buildDescriptor else \
                        (buildDescriptor, "")

Note that this will also throw an Exception when there is more than one "#" in buildDescriptor (which is generally a GOOD thing!)

请注意,当buildDescriptor中存在多个“#”时,这也会抛出异常(这通常是一件好事!)

#3


2  

>>> buildfile, _, target = "hello#world".partition("#")
>>> buildfile, target
('hello', 'world')
>>> buildfile, _, target = "hello".partition("#")
>>> buildfile, target
('hello', '')