简体   繁体   English

ValueError:使用dict格式化时,格式字符\\'“ \\'(0x22)不支持

[英]ValueError: unsupported format character \'"\' (0x22) when formatting with dict

I'm currently working through the O'Reilly book, Programming Python. 我目前正在研究O'Reilly的《 Python编程》一书。 Below is code that reads a shelve and creates a web interface allowing you to access those values from the shelf. 以下是读取货架并创建Web界面的代码,可让您从扩展架访问这些值。 You can fetch and update the values 您可以获取和更新值

'''
Implement a web-based interface for viewing and updating class instances
stored in a shelve; the shelve lives on server (same machine if localhost)
'''

import cgi, sys, os
import shelve, html
shelvename = 'class-shelve'
fieldnames = ('name', 'age', 'job', 'pay')

form = cgi.FieldStorage()
print('Content-type: text/html')
sys.path.insert(0, os.getcwd())

# main html template
replyhtml = """
<html>
<title>People Input Form</title>
<body>
<form method=POST action=peoplecgi.py>
    <table>
    <tr><th>key<td><input type=text name=key value="%(key)">
    $ROWS$
    </table>
    <p>
    <input type=submit value="Fetch", name=action>
    <input type=submit value="Update", name=action>
</form>
</body></html>
"""
# insert html for data rows at $ROWS$
rowhtml = '<tr><th>%s<td><input type=text name=%s value="%%(%s)s">\n'
rowshtml = ""
for fieldname in fieldnames:
    rowshtml += (rowhtml % ((fieldname,)*3))
replyhtml = replyhtml.replace('$ROWS$', rowshtml)


def htmlize(adict):
    new = adict.copy()
    for field in fieldnames:
        value = new[field]
        new[field] = html.escape(repr(value))
    return new

def fetchRecord(db, form):
    try:
        key = form['key'].value
        record = db[key]
        fields = record.__dict__
        fields['key'] = key
    except:
        fields = dict.fromkeys(fieldnames, '?')
        fields['key'] = 'Missing or invalid key!'
    return fields

def updateRecord(db, form):
    if not 'key' in form:
        fields = dict.fromkeys(fieldnames, '?')
        fields['key'] = 'Missing key input!'
    else:
        key = form['key'].value
        if key in db:
            record = db[key]
        else:
            from person_start import Person
            record = Person(name='?', age='?')
        for field in fieldnames:
            setattr(record, field, eval(form[field].value))
        db[key] = record
        fields = record.__dict__
        fields['key'] = key
    return fields

db = shelve.open(shelvename)
action = form['action'].value if 'action' in form else None
if action == 'Fetch':
    fields = fetchRecord(db, form)
elif action == 'Update':
    fields = updateRecord(db, form)
else:
    fields = dict.fromkeys(fieldnames, '?')
    fields['key'] = 'Missing or invalid action!'
db.close()
print(replyhtml % htmlize(fields))

However, for some reason, printing is continually failing. 但是,由于某些原因,打印连续失败。 I've tried many times to remove the "" the error is stating, but to no avail. 我已经尝试过多次以消除错误,但无济于事。

Does anyone see why this is failing to print the form? 有人看到为什么这无法打印表格吗?

After checking the complete code , I believe the issue is in the replyhtml , in line - 检查完完整的代码后,我相信问题出在replyhtml的第-行中-

    <tr><th>key<td><input type=text name=key value="%(key)">

The issue is in the format - "%(key)" . 问题的格式为"%(key)" You need to specify the type of the element like s or d etc , I believe you may need s (for string). 您需要指定sd等元素的类型,我相信您可能需要s (用于字符串)。 Example - 范例-

    <tr><th>key<td><input type=text name=key value="%(key)s">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM