본문 바로가기
파이썬

파이썬의 원시 문자열 리터럴이 단일 백 슬래시로 끝날 수없는 이유는 무엇입니까?

by º기록 2020. 9. 30.
반응형


>>> r'\'
  File "<stdin>", line 1
    r'\'
       ^
SyntaxError: EOL while scanning string literal
>>> r'\\'
'\\\\'
>>> r'\\\'
  File "<stdin>", line 1
    r'\\\'
         ^
SyntaxError: EOL while scanning string literal

파서가 원시 문자열의 백 슬래시를 일반 문자로 처리 할 수있는 것처럼 보이지만 (원시 문자열이 전부가 아닌가?) 분명한 것이 누락되었을 수 있습니다.

 

해결 방법

 

그 이유는 내가 굵게 강조한 부분에 설명되어 있습니다.

문자열 따옴표는 backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line 계속.

따라서 원시 문자열은 100 % 원시가 아니며 여전히 기본적인 백 슬래시 처리가 있습니다.

 

참조 페이지 https://stackoverflow.com/questions/647769

 

 

반응형

댓글