Python方法完成转换英文字符操作

本文是关于Python方法完成转换英文字符操作,用到了webapp2、string、cgi等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

猜你喜欢

转载自blog.csdn.net/lmrylll/article/details/131980682
今日推荐