[Chicago] Getting ASCII encoding where unicode wanted under Py3k

Jonathan Hayward jonathan.hayward at pobox.com
Mon May 13 17:59:33 CEST 2013


I have a Py3k script, pasted below. When I run it I get an error about
ASCII codecs that can't handle byte values that are too high.

The error that I am getting is:

UnicodeEncodeError: 'ascii' codec can't encode character '\u0161' in
position 1442: ordinal not in range(128)
      args = ('ascii', "Content-Type: text/html\n\n<!DOCTYPE
html>\n<html>\n...ype='submit'>\n </form>\n </body>\n</html>", 1442,
1443, 'ordinal not in range(128)')
      encoding = 'ascii'
      end = 1443
      object = "Content-Type: text/html\n\n<!DOCTYPE
html>\n<html>\n...ype='submit'>\n </form>\n </body>\n</html>"
      reason = 'ordinal not in range(128)'
      start = 1442
      with_traceback = <built-in method with_traceback of
UnicodeEncodeError object>

(And that was posted to StackOverflow--one shot in the dark answer so far.)

My code is below. What should I be doing differently to be, in the most
immediate sense, calls to '''%(foo)s''' % locals()?

#!/usr/bin/python3

import cgi
import cgitb;cgitb.enable()
import os
import pickle
import sys

cgi_form = cgi.FieldStorage()

message = ''

def get_cgi(field, default = ""):
    return cgi_form.getfirst(field, default)

try:
    sys.stderr.write('abc: 1')
    input_file = open(
      os.path.join(os.path.dirname(__file__), '../../../russian/pickled'), 'rb')
    sys.stderr.write('abc: 2')
    state = pickle.load(input_file)
    sys.stderr.write('abc: 3')
    state['changed'] = False
    sys.stderr.write('abc: 4')
    state['loaded'] = True
    sys.stderr.write('abc: 5')
except IOError:
    state = {}
    state['phrases'] = []
    state['changed'] = True
    state['loaded'] = False
#except UnicodeDecodeError:
    #state = {}
    #state['phrases'] = []
    #state['changed'] = True
    state['loaded'] = False

if get_cgi('russian') and get_cgi('english'):
    state['phrases'].append([get_cgi('russian'), get_cgi('english')])
    message = 'Your changes have been saved.'
    state['changed'] = True
elif get_cgi('english'):
    state['phrases'].append([None, get_cgi('english')])
    message = 'Your change has been saved.'
    state['changed'] = True

if get_cgi('mode') == 'edit':
    to_delete = []
    for index in range(len(state['phrases'])):
        if get_cgi('russian_' + str(index), None) != None:
            state['phrases'][index][0] = get_cgi('russian_' + str(index))
        if get_cgi('english_' + str(index), None) != None:
            state['phrases'][index][1] = get_cgi('english_' + str(index))
        if get_cgi('delete_' + str(index), None) != None:
            to_delete.insert(0, index)
            #to_delete.append(index)
    #to_delete.sort(lambda a, b: -cmp(a, b))
    for element in to_delete:
        del state['phrases'][element]
    state['changed'] = True

sys.stderr.write('abc: ' + repr(state))

if state['changed']:
    output_file = open(
      os.path.join(os.path.dirname(__file__), '../../../russian/pickled_new.' +
        str(os.getpid())), 'wb')
    pickle.dump(state, output_file)
    output_file.close()
    os.rename(
      os.path.join(os.path.dirname(__file__), '../../../russian/pickled_new.' +
        str(os.getpid())),
      os.path.join(os.path.dirname(__file__), '../../../russian/pickled'))

if get_cgi('mode') == 'add':
    print('''Content-type: text/html

<!DOCTYPE html>
<html>
    <head>
        <meta charset='UTF-8' />
        <style type="text/css">
            body
                {
                font-family: Verdana, Arial, sans;
                }
            input[type=text]
                {
                width: 100%%;
                }
            div.message
                {
                background-color: silver;
                }
        </style>
    </head>
    <body>
        <div class='message'>
            %(message)s
        </div>
        <form action='' method='POST'>
            <p><strong>Russian:</strong><br />
            <input type='text' id='russian' name='russian'></p>

            <p><strong>English:</strong><br />
            <input type='text' id='english' name='english'></p>

            <p><input type='submit'>
        </form>
        <script src='/include/jquery.js'></script>
        <script>
            jQuery('#Russian').focus();
        </script>
    </body>
</html>''' % locals())
elif get_cgi('mode') == 'edit':
    edit_table = '<table>'
    edit_table += '<thead>'
    edit_table += '<th>Russian</th>'
    edit_table += '<th>English</th>'
    edit_table += '<th>Delete</th>'
    edit_table += '</thead>'
    edit_table += '<tbody>'
    for index in range(len(state['phrases'])):
        if state['phrases'][index][0]:
            russian = state['phrases'][index][0].replace('"', "''")
        else:
            russian = None
        english = state['phrases'][index][1].replace('"', "''")
        edit_table += '<tr>'
        edit_table += '<td>'
        if russian != None:
            edit_table += '''<input type='text' name='russian_%(index)d'
            id='russian_%(index)d' value="%(russian)s">''' % locals()
        edit_table += '</td>\n'
        edit_table += '<td>'
        edit_table += '''<input type='text' name='english_%(index)d'
        id='english_%(index)d' value="%(english)s">''' % locals()
        edit_table += '<td>'
        edit_table += '''<input type='checkbox' name='delete_%(index)d'
        id='delete_%(index)d'>''' % locals()
        edit_table += '</td>\n'
        edit_table += '</tr>'
    print ('''Content-Type: text/html

<!DOCTYPE html>
<html>
    <head>
        <title>Edit Phrases</title>
        <meta charset='utf-8' />
    </head>
    <body>
        <form action='' name='edit' id='edit' />
            <input type='hidden' name='mode' value='edit' />
            %(edit_table)s
            <input type='submit'>
        </form>
    </body>
</html>''' % locals())
else:
    text = ''
    for phrase in state['phrases']:
        if phrase[0]:
            text += ('<p title="' + phrase[1].replace('"', "''") + '">' +
              phrase[0] + '</p>')
        else:
            text += '<h2>' + phrase[1] + '</h2>'

    print ('''Content-type: text/html

<!DOCTYPE html>
<html>
    <head>
        <title>___________</title>
        <meta charset='utf-8'>
        <style type='text/css'>
            body
                {
                font-family: Verdana, Arial, sans;
                }
            div
                {
                background-color: #ffff00;
                }
        </style>
        <link rel='stylesheet' type='text/css'
        href='/js/jquery-ui-1.10.2.custom/css/smoothness/jquery-ui-1.10.2.custom.css'
        />
    </head>
    <body>
        %(text)s
        <script src='/js/vendor/jquery-1.8.2-min.js'></script>
        <script
        src='/js/jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js'></script>
        <script>
            jQuery(document).tooltip();
        </script>
    </body>
</html>''' % locals())


-- 
[image: Christos Jonathan Hayward] <http://jonathanscorner.com/>
Christos Jonathan Hayward, an Orthodox Christian author.

*Amazon <http://amazon.com/author/cjshayward>* • Author
Bio<http://jonathanscorner.com/author/>
 • *Author Site <http://cjsh.name/>* •
*Email<christos.jonathan.hayward at gmail.com>
* • Facebook <http://www.facebook.com/christos.jonathan.hayward> • Fan
Page<http://fan.cjshayward.com/>
 • Google Plus <http://jonathanscorner.com/plus> •
LinkedIn<http://www.linkedin.com/in/jonathanhayward>
 • *Professional <http://jonathanhayward.com/>* •
Twitter<http://twitter.com/JonathansCorner>
 • *Web <http://jonathanscorner.com/>* • What's
New?<http://jonathanscorner.com/>
If you read just *one* of my books, you'll want *The Best of Jonathan's
Corner <http://www.amazon.com/dp/1478219912>*.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20130513/aaa0a86e/attachment.html>


More information about the Chicago mailing list