简体   繁体   English

如何将可格式化的字符串传递给 python function?

[英]How do I pass a format-able string to a python function?

I am using a software library that saves its results in a few sound files.我正在使用一个将其结果保存在几个声音文件中的软件库。 I want to save them with a better name in a different directory other than the default one.我想将它们以更好的名称保存在默认目录以外的其他目录中。 The software provides the optional parameter filename_format for this purpose.软件为此提供了可选参数filename_format The docs say to use it this way:文档说以这种方式使用它:

separator.separate_to_file("/path/to/audio.mp3",  "/path/to/output",  filename_format="{filename}_{instrument}.{codec}")

The docs say, "You can user the keyword filename (input file name), instrument (name of the instrument), foldername (name of the folder the input file is in) and codec between curly brackets within the formatting string."文档说:“您可以在格式化字符串中的大括号之间使用关键字文件名(输入文件名)、仪器(仪器名称)、文件夹名(输入文件所在文件夹的名称)和编解码器。”

Here is how I am trying to call the function:这是我尝试调用 function 的方式:

separator.separate_to_file(audioDescriptor, destination, filename_format='{}_{}.{}'.format(basename, "accompaniment", "wav"))

That throws the error:这会引发错误:

spleeter.SpleeterError: Separated source path conflict : /Users/.../separation-component/music/audio_OUTPUT/Yellow_accompaniment.wav,please check your filename format

For reference here is the function I am calling.这里的参考是我打电话的 function。

    def save_to_file(
    self,
    sources: Dict,
    audio_descriptor: AudioDescriptor,
    destination: str,
    filename_format: str = "{filename}/{instrument}.{codec}",
    codec: Codec = Codec.WAV,
    audio_adapter: Optional[AudioAdapter] = None,
    bitrate: str = "128k",
    synchronous: bool = True,
) -> None:
    """
    Export dictionary of sources to files.

    Parameters:
        sources (Dict):
            Dictionary of sources to be exported. The keys are the name
            of the instruments, and the values are `N x 2` numpy arrays
            containing the corresponding intrument waveform, as
            returned by the separate method
        audio_descriptor (AudioDescriptor):
            Describe song to separate, used by audio adapter to
            retrieve and load audio data, in case of file based audio
            adapter, such descriptor would be a file path.
        destination (str):
            Target directory to write output to.
        filename_format (str):
            (Optional) Filename format.
        codec (Codec):
            (Optional) Export codec.
        audio_adapter (Optional[AudioAdapter]):
            (Optional) Audio adapter to use for I/O.
        bitrate (str):
            (Optional) Export bitrate.
        synchronous (bool):
            (Optional) True is should by synchronous.
    """
    if audio_adapter is None:
        audio_adapter = AudioAdapter.default()
    foldername = basename(dirname(audio_descriptor))
    filename = splitext(basename(audio_descriptor))[0]
    generated = []
    for instrument, data in sources.items():
        path = join(
            destination,
            filename_format.format(
                filename=filename,
                instrument=instrument,
                foldername=foldername,
                codec=codec,
            ),
        )
        directory = os.path.dirname(path)
        if not os.path.exists(directory):
            os.makedirs(directory)
        if path in generated:
            raise SpleeterError(
                (
                    f"Separated source path conflict : {path},"
                    "please check your filename format"
                )
            )
        generated.append(path)
        if self._pool:
            task = self._pool.apply_async(
                audio_adapter.save, (path, data, self._sample_rate, codec, bitrate)
            )
            self._tasks.append(task)
        else:
            audio_adapter.save(path, data, self._sample_rate, codec, bitrate)
    if synchronous and self._pool:
        self.join()

See where it's calling看看它在哪里打电话

filename_format.format(
                filename=filename,
                instrument=instrument,
                foldername=foldername,
                codec=codec,
            ),

it's expecting the filename_format arg to contain JUST the string (in your case "{filename}_{instrument}.{codec}" - it does the call to format for you. And it's using named parameters, so you need to put names between curly braces that match the names called in the format call.它期望filename_format arg 只包含字符串(在您的情况下为"{filename}_{instrument}.{codec}" - 它为您调用format 。它使用命名参数,因此您需要将名称放在花括号匹配format调用中调用的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM