我有一个接受sys.argv的脚本,输入可能包含特殊字符(分号)。我只需要输入字符串,但分号把所有东西都搞砸了。。
我有:
def myscript(text)
print text
a = myscript(sys.argv[1])
print a
我尝试:
>> 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""
我得到:
'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
我只想把整个字符串放入脚本中,不管里面有什么。
我试过了:
a = myscript(repr(sys.argv[1]))
a = myscript(str(sys.argv[1]))
这不是python的问题,您需要在调用shell中对其进行转义。只需将引号转义为"
,将分号转义为;
。
$ 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"
这不是python问题,而是bash问题。Bash认为;
(分号)正在分隔一个新的Bash命令。你需要逃离它。