蟒蛇逃脱在sys argv的特殊字符 [英] Python escape special characters in sys argv

查看:222
本文介绍了蟒蛇逃脱在sys argv的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,需要sys.argv中和输入可能包含特殊字符(分号)。我只需要输入的字符串,但分号打乱了一切。

我有:

 高清的MyScript(文本)
    打印文本
一个=的MyScript(sys.argv中[1])
打印一

我尝试:

 >>蟒蛇script.py一边说着,她招呼王子瓦西里库拉金,高级别和重要性的人,谁是第一个在她接待安娜夫娜的到来。已经有一些日子咳嗽。她,正如她所说,痛苦从洛杉矶流行性感冒;流行性感冒是然后在圣彼得堡一个新词

我得到:

 说完这些话,她招呼王子瓦西里库拉金,高级别和重要性的人,谁是第一个在她抵达接待。安娜·帕夫洛夫娜已经有一段日子咳嗽。她,因为她说,'痛苦
没有
庆典:流行性感冒:命令未找到

我只是想获得整个字符串到脚本不管里面是什么吧..

我试过:

  A =的MyScript(再版(sys.argv中[1]))
一个=的MyScript(STR(sys.argv中[1]))


解决方案

这不是蟒蛇的事情,你需要躲避它调用shell。简单地逃避报价为 \\和分号为 \\;

  $蟒蛇testme.py一边说着,她招呼王子瓦西里库拉金,高级别和重要性的人,谁是第一个在她抵达接待安娜夫娜曾经有过咳嗽了几天她,因为她说,从洛杉矶流行性感冒患\\。流行性感冒是然后在圣彼得堡\\一个新词说完这些话,她招呼王子瓦西里库拉金,高级别和重要性的人,谁是第一个在她抵达接待。安娜·帕夫洛夫娜已经有一段日子咳嗽。她,因为她说,患距离la流行性感冒,流行性感冒是然后在圣彼得堡一个新词

I have a script that takes sys.argv and the input may contain special characters (semicolon). I just need the input as string, but semicolon messes everything up..

I have:

def myscript(text)
    print text


a = myscript(sys.argv[1])
print a

I try:

>>  python script.py "With these words she greeted Prince Vasili Kuragin, a man of high rank and importance, who was the first to arrive at her reception. Anna Pavlovna had had a cough for some days. She was, as she said, suffering" from la grippe; grippe being then a new word in St. Petersburg""

I get:

'With these words she greeted Prince Vasili Kuragin, a man of high rank and importance, who was the first to arrive at her reception. Anna Pavlovna had had a cough for some days. She was, as she said, suffering'
None
bash: grippe: command not found

I just want to get the whole string into the script no matter what is inside it..

I tried:

a = myscript(repr(sys.argv[1]))
a = myscript(str(sys.argv[1]))

解决方案

it's not a matter of python, you need to escape it in the calling shell. simply escape quotes as \" and semicolons as \;.

$ python testme.py "With these words she greeted Prince Vasili Kuragin, a man of high rank and importance, who was the first to arrive at her reception. Anna Pavlovna had had a cough for some days. She was, as she said, suffering\" from la grippe; grippe being then a new word in St. Petersburg\""

With these words she greeted Prince Vasili Kuragin, a man of high rank and importance, who was the first to arrive at her reception. Anna Pavlovna had had a cough for some days. She was, as she said, suffering" from la grippe; grippe being then a new word in St. Petersburg"

这篇关于蟒蛇逃脱在sys argv的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆