本文是关于Python方法完成转换英文字符操作,用到了webapp2、string、cgi等python应用方法。
python代码示例写的略微复杂,有兴趣的初学者们可以参考下。
Python方法完成转换英文字符操作,源码如下:
import webapp2
import string
import cgi
form = """
Enter some text to ROT13:
%(text)s
"""
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
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/c2118.html
相关文章 Recommend