Python Pyodbc 二进制列返回 \xda\x08\xcd\x08\xba\x08 而不是数字

Python Pyodbc Binary Column is returning \xda\x08\xcd\x08\xba\x08 instead of numbers

我有一个 SQL 数据库显示像这样的 varbinary (max) 0x9406920691068F...我想将它导入 python pycharm 以获得完全相同的类型数据。 但是,它显示的是这样的 [b'\x94\x06\x92\x06\x91\x06\x8f\x06\x8d.. 如何将相同的数字复制到 python?我是python的初学者,请帮忙

我复制了之前 post 的代码,但没有用

import pyodbc

    def hexToString(binaryString):
    try:
      hashString = ["{0:0>2}".format(hex(b)[2:].upper()) for b in binaryString]
      return '0x' + "".join(hashString)
    except:
      return binaryString


    query = """ select P from Access.table """

conn_str = (
      **** private database details # I don't copy on the page
    )

cnxn = pyodbc.connect(conn_str)
cnxn.add_output_converter(pyodbc.SQL_BINARY, hexToString)
cursor = cnxn.cursor()

try:
    cursor.execute(query)
    row = cursor.fetchone()
except MySQLdb.error as err:
    print(err)
else:
    while row is not None:
        print(row)
        row = cursor.fetchone()

如果列return类型是varbinary(max)那么你需要添加输出转换器函数来处理SQL_VARBINARY,而不是SQL_BINARY

cnxn.add_output_converter(pyodbc.SQL_VARBINARY, converter_function_name)