简体   繁体   English

用于生成Bash脚本的Python脚本中的KeyError

[英]KeyError in Python script used to generate Bash scripts

I have a Python script called renaming.py that I want to use to generate many Bash scripts (over 500). 我有一个名为renaming.py的Python脚本,我想用来生成许多Bash脚本(超过500个)。 The Python script looks like so: Python脚本如下所示:

#!/usr/bin/python

#Script to make multiple Bash scripts based on a .txt file with names of files
#The .txt file contains names of files, one name per line
#The .txt file must be passed as an argument.

import os
import sys

script_tpl="""#!/bin/bash
#BSUB -J "renaming_{line}"
#BSUB -e /scratch/username/renaming_SNPs/renaming_{line}.err
#BSUB -o /scratch/username/renaming_SNPs/renaming_{line}.out
#BSUB -n 8
#BSUB -R "span[ptile=4]"
#BSUB -q normal
#BSUB -P DBCDOBZAK
#BSUB -W 168:00

cd /scratch/username/renaming_SNPs

awk '{sub(/.*/,$1 "_" $3,$2)} 1' {file}.gen > {file}.renamed.gen

"""

with open(sys.argv[1],'r') as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        line = line.strip(".gen")
        script = script_tpl.format(line=line)
        with open('renaming_{}.sh'.format(line), 'w') as output:
            output.write(script)

The .txt file I pass as an argument to this Python script looks like so: 我作为该Python脚本的参数传递的.txt文件如下所示:

chr10.10.merged.no_unwanted.formatted.gen
chr10.11.merged.no_unwanted.formatted.gen
chr10.12.merged.no_unwanted.formatted.gen
chr10.13.merged.no_unwanted.formatted.gen
chr10.14.merged.no_unwanted.formatted.gen
chr10.15.merged.no_unwanted.formatted.gen
etc

When I run the Python script, I get the following error message: 当我运行Python脚本时,出现以下错误消息:

Traceback (most recent call last):
  File "renaming.py", line 33, in <module>
    script = script_tpl.format(line=line)
KeyError: 'sub(/'

I am not entirely sure what is happening, but here is what I think 我不完全确定发生了什么,但这就是我的想法

  • Something is wrong with line 33 - not sure what is the problem. 第33行出现了问题-不确定是什么问题。 I have used very similar scripts like this one before. 之前我曾经使用过非常类似的脚本。 In this line 33, I am replacing all the {line} instances in script_tpl by the entries in the .txt file (this happens 500, once for each line of the .txt file). 在第33行中,我用.txt文件中的条目替换了script_tpl中的所有{line}实例(这种情况发生500次,对于.txt文件的每一行一次)。

  • I am very confused by the KeyError. 我对KeyError感到非常困惑。 I am working on Linux HPC server (using a Mac laptop). 我正在Linux HPC服务器上工作(使用Mac笔记本电脑)。 I have managed to use this awk command with no problem when directly typing it into the terminal (as a Bash command). 我已经设法在直接将其输入终端时(作为Bash命令)毫无问题地使用了这个awk命令。 However, it seems that Python is maybe getting confused when I try and "print" it as a variable in the script.. 但是,当我尝试将其“打印”为脚本中的变量时,似乎Python可能会感到困惑。

Any help would be deeply appreciated. 任何帮助将不胜感激。

When you call str.format it will try to format everything inside {} s. 调用str.format ,它将尝试格式化{}内的所有内容。

So this line is the problem: 所以这行是问题所在:

awk '{sub(/.*/,$1 "_" $3,$2)} 1' {file}.gen > {file}.renamed.gen

Because the string formatter is trying to find the kwargs sub(/ and file in your format call, which don't exist because the only key you specified is line=line . 因为字符串格式化程序正在尝试在格式调用中找到kwargs sub(/file ,所以不存在,因为您指定的唯一键是line=line

If you don't want these to be considered for formatting you need to escape the braces. 如果您不希望这些内容用于格式化,则需要转括号。 (The format call should remove one of the pairs in the final string.) (格式调用应删除最终字符串中的一对。)

awk '{{sub(/.*/,$1 "_" $3,$2)}} 1' {{file}}.gen > {{file}}.renamed.gen

When you use .format all { } in your string will invoke string formatting. 当您使用.format ,字符串中的所有{ }都将调用字符串格式。 Since you used those chars in your awk command, you must escape them. 由于您在awk命令中使用了这些字符,因此必须对其进行转义。 To do that you double the {{ and }} : 为此,您将{{}}加倍:

script_tpl="""#!/bin/bash
#BSUB -J "renaming_{line}"
#BSUB -e /scratch/username/renaming_SNPs/renaming_{line}.err
#BSUB -o /scratch/username/renaming_SNPs/renaming_{line}.out
#BSUB -n 8
#BSUB -R "span[ptile=4]"
#BSUB -q normal
#BSUB -P DBCDOBZAK
#BSUB -W 168:00

cd /scratch/username/renaming_SNPs

awk '{{sub(/.*/,$1 "_" $3,$2)}} 1' {line}.gen > {line}.renamed.gen

"""

Here are the relevant docs . 这是相关的文档

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

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