이것은 매우 직선적 인 python입니다.
for 루프는 반복 가능 항목에서 개별 항목의 압축을 풉니 다. 따라서, 예를 들어, 당신은 다음과 같이가 someting을 할 수있는 : 귀하의 경우 (i, o)
에
>>> c = [('a', 'b', 'c'), ('d', 'e', 'f')]
>>> for i, j, k in c:
... print i, j, k
...
a b c
d e f
는 튜플의 튜플의 값으로되고있다. i
의 인스턴스는 o
의 값으로 대체됩니다. 이 함수는 html 특수 문자를 각각 나타내는 엔티티로 대체합니다.
>>> s = 'foo & bar'
>>> s = s.replace('&', '&')
>>> s
'foo & bar'
이 기능은 동등하고있다가 : 무엇을보고 어떤 인쇄 문을 추가하려고 적절한 디버거를 사용하는 대신에
def escape_html(s):
s = s.replace("&","&")
s = s.replace(">", ">")
s = s.replace("<", "<")
s = s.replace('"', """)
return s
이 벌어지고
:
def escape_html(s):
print "ORIGINAL STRING: %s" % (s)
for (i, o) in (("&","&"),(">", ">"),("<", "<"),('"', """)):
print "\t(i, o) = ('%s', '%s')" % (i, o)
s = s.replace(i , o)
print "\ts = %s" % (s,)
print
return s
mystring = """<h3>This is a test</h3><script>alert("I hacked you!");</script>"""
print escape_html(mystring)
OUTPUT
ORIGINAL STRING: <h3>This is a test</h3><script>alert("I hacked you!");</script>
(i, o) = ('&', '&')
s = <h3>This is a test</h3><script>alert("I hacked you!");</script>
(i, o) = ('>', '>')
s = <h3>This is a test</h3><script>alert("I hacked you!");</script>
(i, o) = ('<', '<')
s = <h3>This is a test</h3><script>alert("I hacked you!");</script>
(i, o) = ('"', '"')
s = <h3>This is a test</h3><script>alert("I hacked you!");</script>
<h3>This is a test</h3><script>alert("I hacked you!");</script>
"루프의 내용"은 문자열의 각 인스턴스를 해당 'o'문자열로 바꾸는 것입니다. – Amit
@AmitMizrahi 감사합니다. for 루프 헤더에 집중했습니다. – Cogwheel