Python方法完成转换英文字符操作,,本文是关于Python方


本文是关于Python方法完成转换英文字符操作,用到了webapp2、string、cgi等python应用方法。

python代码示例写的略微复杂,有兴趣的初学者们可以参考下。

Python方法完成转换英文字符操作,源码如下:

import webapp2import stringimport cgiform = """<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_textapp = webapp2.WSGIApplication([('/rot13', Rot13)],                              debug=True)

Python语言方法,完成转换英文字符的操作示例。

application: udacity-cs253-zfzversion: 1runtime: python27api_version: 1threadsafe: truehandlers:- url: /.*   script: rot13.app

编橙之家文章,

评论关闭