Экранирование специальных символов Python в sys argv - PullRequest
1 голос
/ 06 марта 2012

У меня есть скрипт, который принимает 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]))

Ответы [ 2 ]

7 голосов
/ 06 марта 2012

это не вопрос Python, вам нужно экранировать его в вызывающей оболочке.просто экранируйте кавычки как \" и точки с запятой как \;.

$ 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"
2 голосов
/ 06 марта 2012

Это не проблема с питоном, это bash проблема.Bash считает, что ; (точка с запятой) разделяет новую команду bash.Вам нужно избежать этого.

...