Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error sending an argument with an ampersand character in the value in python?

I'm dealing 2 scripts in python. the first one needs to send a value or an argument to the second script. Now the problem is whenever I send the value to the 2nd script, the 2nd script couldn't get all the arguments that I sent. the value that Im sending is a URL and it contains an ampersand. I noticed that it kept on cutting the value to the first appearance of &.

lets sat for example, I need to pass this :

http://www.google.com/jfljflfjej&12345

the 2nd script will receive only this :

http://www.google.com/jfljflfjej

what do I need to do to be able to catch the correct value? And what other characters that have the same issue as this?

like image 492
srh snl Avatar asked Sep 21 '12 06:09

srh snl


2 Answers

You need to put quotes around the whole value, including the ampersand, as it is a special shell character. Alternatively, you can escape just the ampersand by putting a backslash in front of it:

http://www.google.com/jfljflfjej\&12345

The ampersand signals to the shell you want to put the command up to that point in background mode.

Any of the following characters are special in a shell:

 \ ' " ` < > | ; <Space> <Tab> <Newline> ( ) [ ] ? # $ ^ & * =

These need to be escaped in the same way; use a backslash or put quotes around the value.

like image 179
Martijn Pieters Avatar answered Oct 07 '22 01:10

Martijn Pieters


Maybe it's a bit late to answer you but I had the same problem and I found a simple solution to that.

You can use the function replace() to replace every ampersand with their code in HTML '%26' like this.

url = http://www.google.com/jfljflfjej&12345
print url.replace('&', '%26')

And the result:

http://www.google.com/jfljflfjej%2612345

It's a problem of coding.

like image 31
Joanmacat Avatar answered Oct 06 '22 23:10

Joanmacat