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

3

5

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?

Bondie answered 21/9, 2012 at 6:24 Comment(1)
How is your script1 calling script2?Scarlett
C
6

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.

Corenecoreopsis answered 21/9, 2012 at 6:28 Comment(0)
E
2

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.

Extrusion answered 8/4, 2014 at 14:9 Comment(0)
S
0

In Python 3:

import urllib.parse
url = urllib.parse.quote("http://www.google.com/jfljflfjej&12345")
Survive answered 14/7, 2022 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.