Python &引用;“未找到子字符串”;即使它';包含在命令行上传递的参数中

Python &引用;“未找到子字符串”;即使它';包含在命令行上传递的参数中,python,ubuntu,Python,Ubuntu,我不明白为什么会发生这种情况,所以非常感谢您的帮助 你好 然后在ubuntu中,如果我尝试运行以下内容: python hi.py http://somewebsite.com/api?token=hello_world&user=myself 我将此作为输出: http://somewebsite.com/api?token=hello_world 27 Traceback (most recent call last): File "hi.py", line 10, in &l

我不明白为什么会发生这种情况,所以非常感谢您的帮助

你好

然后在ubuntu中,如果我尝试运行以下内容:

python hi.py http://somewebsite.com/api?token=hello_world&user=myself
我将此作为输出:

http://somewebsite.com/api?token=hello_world
27
Traceback (most recent call last):
  File "hi.py", line 10, in <module>
    print url.index("&user")
ValueError: substring not found
http://somewebsite.com/api?token=hello_world
27
回溯(最近一次呼叫最后一次):
文件“hi.py”,第10行,在
打印url.index(“&user”)
ValueError:未找到子字符串

由于某些原因,没有打印整个URL,我怀疑这就是找不到&user子字符串的原因。有什么想法吗?

&
是一个shell元字符,它是。您需要引用您的参数以防止
&
字符被解释为:

python hi.py”http://somewebsite.com/api?token=hello_world&user=myself"
或者使用
\
反斜杠仅转义与符号:

python hi.pyhttp://somewebsite.com/api?token=hello_world\&用户=我自己
否则,shell会将其视为两个独立的命令,
user=my
恰好是有效的shell语法

以下任一字符为:

“`<>|()[]?\$^&*=
这些需要以相同的方式转义;
恰好适用于URL,因为没有匹配的文件名;如果您使用了
hi.p?
文件名
hi.py
将被替换。
=
仅在左侧是有效的shell变量标识符时才需要转义,但为了安全起见,我还是坚持使用它引用整个URL。

&
是一个shell元字符,需要引用您的参数以防止
&
字符被解释为:

python hi.py”http://somewebsite.com/api?token=hello_world&user=myself"
或者使用
\
反斜杠仅转义与符号:

python hi.pyhttp://somewebsite.com/api?token=hello_world\&用户=我自己
否则,shell会将其视为两个独立的命令,
user=my
恰好是有效的shell语法

以下任一字符为:

“`<>\”;( ) [ ] ? # $ ^ & * =
这些需要以同样的方式逃脱;
恰好适用于URL,因为没有匹配的文件名;如果您使用了
hi.p?
文件名
hi.py
将被替换。我认为,只有当左边是有效的shell变量标识符时,
=
才需要转义,但为了安全起见,我会坚持引用整个URL。

&
是一个shell元字符,它是一个。您需要引用您的参数以防止
&
字符被解释为:

python hi.py”http://somewebsite.com/api?token=hello_world&user=myself"
或者使用
\
反斜杠仅转义与符号:

python hi.pyhttp://somewebsite.com/api?token=hello_world\&用户=我自己
否则,shell会将其视为两个独立的命令,
user=my
恰好是有效的shell语法

以下任一字符为:

“`<>|()[]?\$^&*=
这些需要以相同的方式转义;
恰好适用于URL,因为没有匹配的文件名;如果您使用了
hi.p?
文件名
hi.py
将被替换。
=
仅在左侧是有效的shell变量标识符时才需要转义,但为了安全起见,我还是坚持使用它引用整个URL。

&
是一个shell元字符,需要引用您的参数以防止
&
字符被解释为:

python hi.py”http://somewebsite.com/api?token=hello_world&user=myself"
或者使用
\
反斜杠仅转义与符号:

python hi.pyhttp://somewebsite.com/api?token=hello_world\&用户=我自己
否则,shell会将其视为两个独立的命令,
user=my
恰好是有效的shell语法

以下任一字符为:

“`<>\”;( ) [ ] ? # $ ^ & * = 这些需要以同样的方式逃脱;
恰好适用于URL,因为没有匹配的文件名;如果您使用了
hi.p?
文件名
hi.py
将被替换。我认为,
=
只有在左侧是有效的shell变量标识符时才需要转义,但为了安全起见,我还是坚持引用整个URL

http://somewebsite.com/api?token=hello_world
27
Traceback (most recent call last):
  File "hi.py", line 10, in <module>
    print url.index("&user")
ValueError: substring not found