[Python] Access Python dictionary values by a shell variable within Snakemake shell commands

Discussão em 'Python' iniciado por Stack, Fevereiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    I tried to use a shell variable to access Python dictionary values inside the shell commands but got an error. The following is a simplified example in which $x needs to be used:

    my_dict = {}
    my_dict['SHELL_KEY'] = 'key3'
    print(my_dict)

    rule another_rule:
    output: "output.txt"
    shell:
    """
    x='SHELL_KEY'
    echo {my_dict[$x]} > {output}
    """

    RuleException in rule another_rule in file /home/zhujack/workflow/ngs_aPipe_snake_dev/rules/align.rules, line 43:
    NameError: The name '$x' is unknown in this context. Please make sure that you defined that variable. Also note that braces not used for variable access have to be escaped by repeating them, i.e. {{print $1}}, when formatting the following:

    x='SHELL_KEY'
    echo {my_dict[$x]} > {output}

    Continue reading...

Compartilhe esta Página