2013-03-19 2 views
0

사용자가 이스케이프 문자 (예 : "Example \"또는 "C : \ Users ...")를 포함하는 문자열을 입력하는 경우 그대로 받아들입니다. . 상기와 같은 입력이 처리 될 때 나는 간단해야 같은 느낌이스케이프 문자를 포함하는 문자열을 받아들이는 방법 python

던져 구문 오류를 회피하기 위해 노력하고있어,하지만 대답은 어떤 도움 저를 회피한다

편집 :.? 나는에있어 python 3

+0

당신은'사용하고 대부분의 경우 'raw_input' (또는 Python 3.x)을 사용하고자 할 때, Python 2.x에서 'input'을 사용합니다. – abarnert

+0

파이썬 3, 편집 된 게시물 – pipsqueaker117

+1

파이썬 3에서'input()'호출로'SyntaxError'를 얻는 방법을 모르겠다. (당신이하는 일을 가정하면 - 그렇지 않다. "처리되는"것을 의미하는 코드를 보여줍니다.)'import sys'와'print (sys.version)'을 프로그램에 추가하여 여러분이 생각하는 파이썬을 사용하고 있는지 확인하십시오. – DSM

답변

5

input()을 사용하지 말고, 문자열 입력을받을 때 대신 raw_input()을 사용하십시오.

파이썬 3 일

>>> raw_input('Please show me how this works: ') 
Please show me how this works: This is \n how it works! 
'This is \\n how it works!' 

:

input() (파이썬 2)는 이스케이프 시퀀스로 \ 백 슬래시를 해석하려고하지를 포함한 모든 텍스트를 해석하려고하지 않습니다 raw_input(), 파이썬 등의 입력 문자열을 해석하려고 input() (파이썬 2의 raw_input() 이름이 바뀜) 만 사용하십시오. 당신은 또한 eval()를 사용하지 않는 한 그것은 이스케이프 코드를 해석하지됩니다

>>> input('Please show me how this works: ') 
Please show me how this works: This is \n how it works! 
'This is \\n how it works!' 
+0

죄송합니다. 지정 했어야합니다. 나는 파이썬 3에있다. – pipsqueaker117

+0

@ pipsqueaker117 : 업데이트. 파이썬 3'input()'은 파이썬 2의'raw_input()'함수와 같은 함수입니다. –

+0

오. 고맙습니다. – pipsqueaker117

0

특수 문자와 문자열을 받아 파이썬 2.7 사용에 raw_input()를 사용하는 경우.

샘플 프로그램

$vi demo.py 

str1=raw_input("Enter the file with full path : ") 

print "Given Path is : ",str 

save and quit from vi editor 

Output 

$python demo.py 

Enter the path :/home/ubuntu/deveops 

Given path is : /home/ubuntu/deveops 

$ python demo.py 

Enter the path :\home\ubuntu\deveops 

Given path is : \home\ubuntu\deveops 

당신은 python3 doesnot raw_input을했기 때문에 특수 문자와 문자열을 받아 python3 사용 입력()를 사용하는 경우() 함수

$vi demo1.py 

str1=input("Enter the file with full path : ") 

print ("Given Path is : ",str1) 

save and quit from vi editor 

Output 

$python demo1.py 

Enter the path :/home/ubuntu/deveops 

Given path is : /home/ubuntu/deveops 


$ python demo1.py 

Enter the path :\home\ubuntu\deveops 

Given path is : \home\ubuntu\deveops 
+0

[markdown editing] (https://stackoverflow.com/editing-help)을 사용하십시오. –

관련 문제