이 기사는 webapp2, string, cgi 등과 같은 Python 응용 프로그램 방법을 사용하여 영어 문자 변환을 완료하는 Python 방법에 관한 것입니다.
Python 코드 예제는 작성하기가 약간 복잡하므로 관심 있는 초보자는 참고할 수 있습니다.
Python 방식으로 영문자의 변환을 완료하며 소스코드는 다음과 같다.
import webapp2
import string
import cgi
form = """
<form method="post">
<b>Enter some text to ROT13:</b> <br><br>
<textarea name="text" rows="5" cols="50">%(text)s</textarea>
<br><br>
<input type="submit">
</form>
"""
def escape_html(s):
return cgi.escape(s, quote = True)
def translate(s):
old = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
new = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
return string.translate(s, string.maketrans(old, new))
class Rot13(webapp2.RequestHandler):
def write_form(self, text=""):
self.response.out.write(form % {"text": text})
def get(self):
self.write_form()
def post(self):
user_text = self.request.get("text")
user_text = translate(str(user_text))
#print user_text
user_text = escape_html(user_text)
self.write_form(user_text)
#print user_text
app = webapp2.WSGIApplication([('/rot13', Rot13)],
debug=True)
Python 언어 방식으로 영어 문자 변환 작업 예제를 완성합니다.
application: udacity-cs253-zfz
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: rot13.app