【问题标题】:Not getting the final output in the python program for web dev没有在用于 web 开发的 python 程序中获得最终输出
【发布时间】:2017-11-10 12:42:46
【问题描述】:

我编写了以下代码来打印字符串的 rot13 代码。 Rot13 代码是将字符串的字母移动 13 位的地方。但是由于某种原因,打印的文本框总是空白的。有人可以帮我处理这段代码吗?

import os
import webapp2
import jinja2
import cgi
import string

template_dir= os.path.join(os.path.dirname(__file__), 'templates')
jinja_env= jinja2.Environment(loader= jinja2.FileSystemLoader(template_dir),
                                                                         autoescape= True) # automatically escapes all the variables


form="""
<form method="post">
      <textarea name="text" style="height: 100px; width: 400px;">%(val)s</textarea>
      <br>
      <input type="submit">
    </form>
"""

class Handler(webapp2.RequestHandler):
    def write(self,*a,**kw):
        self.response.out.write(*a,**kw)

    def render_str(self,template,**params):
        #Causes Jinja to load that file creating jinja template
        t=jinja_env.get_template(template)
        return t.render(params)

    def render(self,template,**kw):
        self.write(self.render_str(template,**kw))



class MainPage(webapp2.RequestHandler):

    def escape_html(self,s):
        return cgi.escape(s, quote=True)

    def write_form(self, s=''):
        self.response.out.write(form % {"val":s})

    def get(self):
        self.write_form()

    def rot13(self, s):
        out=""
        str1 =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
        str2 = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
        for c in s:
            if c in str1:
                k = str1.index(c)
                out += string.replace( s,c, str2[k])
            else:
                out += c
        return out

    def post(self):
        user_text = self.request.get('text')
        user_text = self.escape_html(user_text)
        user_text = self.rot13(user_text)


        self.write_form(user_text)


app = webapp2.WSGIApplication([
('/', MainPage)
], debug=True)

【问题讨论】:

    标签: python python-2.7 jinja2 google-app-engine-python


    【解决方案1】:

    你可以使用:

    import codecs
    

    然后进入 POST,您可以使用以下方法直接将字符串转换为 rot_13:

    text = codecs.encode(user_text, 'rot_13')
    

    这里修改的代码:

    import os
    import webapp2
    import jinja2
    import cgi
    import string
    import codecs
    
    template_dir= os.path.join(os.path.dirname(__file__), 'templates')
    jinja_env= jinja2.Environment(loader= 
    jinja2.FileSystemLoader(template_dir),
    
    autoescape= True) # automatically escapes all the variables
    
    
    form="""
    <form method="post">
      <textarea name="text" style="height: 100px; width: 400px;">%
      (val)s</textarea>
      <br>
      <input type="submit">
    </form>
    """
    
    class Handler(webapp2.RequestHandler):
       def write(self,*a,**kw):
          self.response.out.write(*a,**kw)
    
       def render_str(self,template,**params):
          #Causes Jinja to load that file creating jinja template
          t=jinja_env.get_template(template)
          return t.render(params)
    
       def render(self,template,**kw):
          self.write(self.render_str(template,**kw))
    
    class MainPage(webapp2.RequestHandler):
    
        def escape_html(self,s):
            return cgi.escape(s, quote=True)
    
        def write_form(self, s=''):
            self.response.out.write(form % {"val":s})
    
        def get(self):
           self.write_form()
    
    def post(self):
        user_text = self.request.get('text')
        text = codecs.encode(user_text, 'rot_13')
        self.write_form(text)
    
    
    app = webapp2.WSGIApplication([
    ('/', MainPage)
    ], debug=True)
    

    这里是应用运行的结果动画:

    编辑: 如果你想使用你的例程:

    def rot13(self, s):
        out = ""
        str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
        str2 = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
        for c in s:
            if c in str1:
                k = str1.index(c)
                #out += string.replace(s, c, str2[k])
                out += str2[k]
            else:
                out += c
        return out
    

    【讨论】:

    • 非常感谢。这为我做了工作。但我无法理解我的代码有什么问题。你能帮我解决这个问题吗?
    • 如果你想使用你的例程,请确定@Taylor 问题在于 out += string.replace(s, c, str2[k])。您只需要连接转换 += str2[k]。如果你愿意,我可以将修改添加到代码中。
    • 非常感谢@Nicolas!这确实很有帮助!
    • 伟大的@Taylor 很高兴听到这个消息!
    • @Taylor 如果贡献有用请投票。你有正面或负面的2个选项:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2015-12-10
    • 2017-09-27
    • 2012-09-10
    • 2014-03-25
    • 1970-01-01
    相关资源
    最近更新 更多