TypeError:传递给bytes .__ format__的不支持的格式字符串

时间:2017-07-25 02:27:43

标签: python sqlite

您好,我试图将sqlite文件打印为可读格式。我正在使用dionea honeynet的python脚本。每次运行脚本时都会遇到此错误:

File "./readlogsqltree.py", line 268, in print_logins
    login['login_password']))

TypeError: unsupported format string passed to bytes.__format__

登录[' login_password']是取自select语句的变量。这是被调用的函数:

def print_logins(cursor, connection, indent):
    r = cursor.execute("""
        SELECT 
            login_username,
            login_password
        FROM 
            logins
        WHERE connection = ?""", (connection, ))
    logins = resolve_result(r)
    for login in logins:
        print("{:s} login - user:'{:s}' password:'{:s}'".format(
            ' ' * indent,
            login['login_username'],
            login['login_password']))

解析结果函数:

def resolve_result(resultcursor):
    names = [resultcursor.description[x][0] for x in range(len(resultcursor.description))]
    resolvedresult = [ dict(zip(names, i)) for i in resultcursor]
    return resolvedresult

光标:

def print_db(opts, args):
    dbpath = 'logsql.sqlite'
    if len(args) >= 1:
        dbpath = args[0]
    print("using database located at {0}".format(dbpath))
    dbh = sqlite3.connect(dbpath)
    cursor = dbh.cursor()

2 个答案:

答案 0 :(得分:1)

将某些python2脚本转换为python3时遇到了此错误:

  File "/home/dog/Documents/proiecte/tf-booking/tensorflow-object-detection-example/object_detection_app/app.py", line 152, in encode_image
    base64.b64encode(image_buffer.getvalue()))
TypeError: unsupported format string passed to bytes.__format__

解决方案是使用base64.b64encode(image_buffer.getvalue()).decode()代替this post中的base64.b64encode(image_buffer.getvalue())

答案 1 :(得分:0)

对于Python 3,在调用decode('utf-8')之前使用format()解码字符串