我有一个带有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]))
答案 0 :(得分:7)
这不是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"
答案 1 :(得分:2)
这不是python问题,而是 bash 问题。 Bash认为;
(分号)正在分离一个新的bash命令。你需要逃脱它。