0

I am writing a python script, from I am trying to call perl script. The input to the perl scripts are python arguments.

 row[i[k]]=re.sub(r"^_", "", row[i[k]])
 row[i[k]]=re.sub(r"{", "[", row[i[k]])
 row[i[k]]=re.sub(r"}", "]", row[i[k]])
 row[i[0]]=re.sub(r"^_", "", row[i[0]])
 row[i[0]]=re.sub(r"{", "[", row[i[0]])
 row[i[0]]=re.sub(r"}", "]", row[i[0]])
 cmd = "perl process_str.pl -str1 "row[i[0]]" -str2 "row[i[k]]""
 os.system(cmd)

But I'm seeing following error on running the python script:

cmd = "perl process_str.pl -str1 "row[i[0]]" -str2 "row[i[k]]""
                                ^
SyntaxError: invalid syntax
3
  • Put simple quote instead of double for the inner quotes cmd = "perl process_str.pl -str1 'row[i[0]]' -str2 'row[i[k]]'" Commented Oct 16, 2020 at 17:22
  • 1
    Removed Perl tag as nothing about this question is specific to Perl. You are building a shell command Commented Oct 16, 2020 at 17:22
  • 1
    And don't do what azro suggests. 1) That's would not interpolate the values but pass the expressions as-is, and 2) If it did interpolate, it would suffer from a code injection bug Commented Oct 16, 2020 at 17:23

1 Answer 1

2

The following can be used:

subprocess.call([ "perl", "process_str.pl", "-str1", row[i[0]], "-str2", row[i[k]] ])

There's no point invoking a shell to launch perl when we can launch perl directly. And since we avoid using a shell, we avoid having to build a shell command.

Sign up to request clarification or add additional context in comments.

6 Comments

Output? You mean what the program prints to stdout? You want to print to stdout what the program prints to stdout? Well, hmmm, that already happens.
the output of perl script is string and when i run pythin it is not giving any outputs
No. If it's not outputting anything, it's not outputting anything.
I have run perl script independently and it has worked properly for me. Then when i include the perl script in python it is nt working.
perl process_str.pl -str1 "iod.BP_0_D[104,44,32,20,8,105,45,33,21,9]" -str2 "MCA_DQS_H[9]_RSVD),(MCA_DQS_H[8:5]_MCA_DM[3:0]),MCA_DQS_H[4:0]" BP_0_D[104] , MCA_DQS_H_9_RSVD_ BP_0_D[44] , MCA_DQS_H_8_MCA_DM_3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.