title: Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 ordinal not in range(128)
body: <p>I'm trying to write my first app in Google App Engine with Python (link of the app: <a href="http://contractpy.appspot.com/" rel="nofollow noreferrer">http://contractpy.appspot.com/</a> - it's just an experimental app). The entire code is bellow. </p>
  
  <p>But, when I submit the data, I'm getting this error (showed on logs):</p>
  
  <pre><code>(...) line 265, in get "contractType":geted_contractType
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 949: ordinal not in range(128)
  </code></pre>
  
  <p>Line 265 is in this if block:</p>
  
  <pre><code>self.response.out.write(yourcontract % {"resident":geted_resident_name,
                                        "nacionality":geted_user_nacionality,
                                        "SSN":geted_user_SSN,
                                        "SSN_error":geted_SSN_error,
                                        "driverLicense":geted_user_driverLicense,
                                        "email":geted_user_email,
                                        "witness ":geted_witness ,
                                        "owner":geted_owner,
                                        "contractType":geted_contractType
                                        })
  </code></pre>
  
  <p>I tried to make changes, read similar awnswers (like <a href="https://stackoverflow.com/questions/10597677/python-concatenating-strings-unicodedecodeerror-ascii-codec-cant-decode-by">this</a> or <a href="https://stackoverflow.com/questions/10561923/unicodedecodeerror-ascii-codec-cant-decode-byte-0xef-in-position-1">this</a>), but nothing helped. I can't figure out in this code what is going wrong. Can someone figure out what is causing such error and how to fix it?</p>
  
  <p>I'm using Python 2.7. </p>
  
  <p>Thanks in advance for any help!</p>
  
  <pre><code># -*- coding: utf-8 -*-
  # -*- coding: utf-8 -*-
  #!/usr/bin/env python
  #
  # Copyright 2007 Google Inc.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  #
  
      import webapp2
  
      form = """
  
      &lt;html&gt;
        &lt;head&gt;
          &lt;title&gt;Contract with Python&lt;/title&gt;
          &lt;style type="text/css"&gt;
            .label {text-align: right}
            .error {color: red}
          &lt;/style&gt;
  
        &lt;/head&gt;
  
        &lt;body&gt;
          &lt;h2&gt;Contract with Python&lt;/h2&gt;
          &lt;form method="post"&gt;
            &lt;table&gt;
              &lt;tr&gt;
                &lt;td class="label"&gt;
                  resident
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;input type="text" name="resident" value= "%(resident)s"&gt;
                &lt;/td&gt;
                &lt;td class="error"&gt; %(resident_error)s
  
                &lt;/td&gt;
              &lt;/tr&gt;
  
               &lt;tr&gt;
                &lt;td class="label"&gt;
                  nacionality
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;input type="text" name="nacionality" value= " "&gt;
                &lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                &lt;td class="label"&gt;
                  License
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;input type="text" name="driverLicense" value=""&gt;
                &lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;
                &lt;td class="label"&gt;
                  SSN
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;input type="text" name="SSN" value="%(SSN)s"&gt;
                &lt;/td&gt;
                &lt;td class="error"&gt; %(SSN_error)s
                &lt;/td&gt;
              &lt;/tr&gt;
  
  
              &lt;tr&gt;
                &lt;td class="label"&gt;
                  Email (optional)
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;input type="text" name="email" value="%(email)s"&gt;
                &lt;/td&gt;
                &lt;td class="error"&gt; %(email_error)s
  
                &lt;/td&gt;
              &lt;/tr&gt;
            &lt;/table&gt;
  
      &lt;br&gt;
              &lt;td class="label"&gt;
                Contract Type
                &lt;select name = "contractType"&gt;
                    &lt;option&gt;House  Rental Contract&lt;/option&gt;
                    &lt;option&gt;Car Rental Contract&lt;/option&gt;
                    &lt;option&gt;Other&lt;/option&gt;
                &lt;/select&gt;
                &lt;/td&gt;
      &lt;br&gt;
      &lt;br&gt;
  
      &lt;br&gt;
                owner
                &lt;select name = "owner"&gt;
                    &lt;option&gt;House owner&lt;/option&gt;
                    &lt;option&gt;Car owner&lt;/option&gt;
                    &lt;option&gt;Other owner&lt;/option&gt;
                &lt;/select&gt;
      &lt;br&gt;
      &lt;br&gt;
  
      &lt;br&gt;
                Witness
                &lt;select name = "witness"&gt;
                    &lt;option&gt;Carl Sagan&lt;/option&gt;
                    &lt;option&gt;Mahatma Gandhi&lt;/option&gt;
                &lt;/select&gt;
      &lt;br&gt;
      &lt;br&gt;
  
            &lt;input type="submit"&gt;
          &lt;/form&gt;
        &lt;/body&gt;
  
      &lt;/html&gt;
      """
  
      yourcontract = """
      &lt;html&gt;
        &lt;head&gt;
          &lt;title&gt;Unit 2 Signup&lt;/title&gt;
        &lt;/head&gt;
  
        &lt;body&gt;
  
      %(contractType)s
  
      In consideration of the agreements of the Resident(s), known as: %(resident)s The owner hereby rents them the dwelling located at %(residentAdress)s, for the period commencing on the %(dateStarts)s, and monthly thereafter until the last day of %(dateEnds)s, at which time this Agreement is terminated. Resident(s), in consideration of owners permitting them to occupy the above property, hereby agrees to the following terms:
  
      RENT: To pay as rental the sum of $ (rentalSum) per month, due and payable in advance from the first day of every month. Failure to pay rent when due will result in the owner taking immediate legal action to evict the Resident from the premises and seize the security deposit.
      LATE FEE: Rent received after the first of the month will be subject to a late fee of 10% plus (3.00) dollars per day.
  
      ACCEPTED THIS (dateContract), at (localContract).
  
      ___________________________________________________
      %(resident)s - Resident
  
      ___________________________________________________
      %(owner)s – owner
  
      ___________________________________________________
      %(witness)s – Witness
  
        &lt;/body&gt;
      &lt;/html&gt;
  
      """
  
      import re
  
      USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
      def valid_resident(resident):
          return USER_RE.match(resident)
  
      PASS_RE = re.compile(r"^.{3,20}$")
      def valid_SSN(SSN):
          return PASS_RE.match(SSN)
  
      EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$")
      def valid_email(email):
          return EMAIL_RE.match(email)
  
      def escape_html(s):
          for (i,o) in (("&amp;","&amp;"), ("&gt;","&gt;"), ("&lt;","&lt;"), ('"','"')):
              s = s.replace(i,o)
              return s
  
      import time
  
      import datetime
  
      def dateToday():
          today = datetime.datetime.today()
          todayDay = str(today.day)
          todayMonth = str(today.month)
          monthExt = {'1':' January ', '2':'February', '3':' March ', '4':'April', '5':'May', '6':'June', '7 ':' July ', '8':'August', '9':'September', '10':'October', '11':'November ', '12':'December'}
          todayYear = str(today.year)
          return(todayDay + ' of  ' + monthExtenso[todaymonth] + ' of ' + todayYear)
  
      class MainHandler(webapp2.RequestHandler):
          def get(self):
             self.response.out.write(form %{"resident": "",
                                             "SSN": "",
                                             "driverLicense": "",
                                             "email":"",
                                             "resident_error": "",
                                             "SSN_error": "",
                                             "driverLicense_error": "",
                                             "email_error": ""})
  
          def write_form(self, text_write):
             self.response.out.write(text_write)
  
          def post(self):
              resident_name = self.request.get(escape_html("resident"))
              user_nacionality = self.request.get("nacionality")
              user_SSN = self.request.get(escape_html('SSN'))
              user_email = self.request.get(escape_html('email'))
              user_driverLicense = self.request.get(escape_html('driverLicense'))
              resident_error = ""
              SSN_error = ""
              driverLicense_error = ""
              contract_type = self.request.get("contractType")
              owner = self.request.get("owner")
              witness  = self.request.get("witness ")
  
              if (resident_name and valid_resident(resident_name)) \
              and (user_SSN and valid_SSN(user_SSN)) \
              and ((not user_email) or (user_email and valid_email(user_email))):
                  self.redirect('/yourcontract?resident=%s&amp;nacionality=%s&amp;SSN=%s&amp;driverLicense=%s&amp;email=%s&amp;witness=%s&amp;owner=%s' % (resident_name, user_nacionality, user_SSN, user_driverLicense, user_email,
      witness, owner))
              else:
                  if not valid_resident(resident_name):
                      resident_error = "Oh no!!! this resident name isn't valid!"
                  if not valid_SSN(user_SSN):
                      SSN_error = "Oh no!!! SSN isn't valid!"
                  if user_email and not valid_email(user_email):
                      email_error = "Oh no!!! e-mail isn't valid!"
                  self.write_form(form % {"resident":resident_name,
                                            "resident_error":resident_error,
                                            "SSN":user_SSN,
                                            "SSN_error":SSN_error,
                                            "driverLicense":user_driverLicense,
                                            "email":user_email,
                                            })
  
      class yourcontractHandler(webapp2.RequestHandler):
          def get(self):
              geted_resident_name = self.request.get('resident')
              geted_user_nacionality = self.request.get("nacionality")
              geted_user_SSN = self.request.get('SSN')
              geted_user_email = self.request.get('email')
              geted_user_driverLicense = self.request.get('driverLicense')
              geted_resident_error = ""
              geted_SSN_error = ""
              geted_driverLicense_error = ""
              #geted_contract_type = self.request.get("contractType")
              geted_owner = self.request.get("owner")
              geted_witness  = self.request.get("witness")
              geted_contractType = self.request.get("contractType")
  
  
  
              self.response.out.write(yourcontract % {"resident":geted_resident_name,
                                            "nacionality":geted_user_nacionality,
                                            "SSN":geted_user_SSN,
                                            "SSN_error":geted_SSN_error,
                                            "driverLicense":geted_user_driverLicense,
                                            "email":geted_user_email,
                                            "witness ":geted_witness ,
                                            "owner":geted_owner,
                                            "contractType":geted_contractType
                                            })
  
  
      app = webapp2.WSGIApplication([('/', MainHandler), ('/yourcontract', yourcontractHandler)],
                                    debug=True)
  </code></pre>