简体   繁体   English

Python Pyodbc Binary Column返回\\ xda \\ x08 \\ xcd \\ x08 \\ xba \\ x08而不是数字

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

I have a SQL database that displays a varbinary (max) like this 0x9406920691068F... I want to import it to python pycharm to get the same exact type of data. 我有一个显示这样的varbinary(max)的SQL数据库,例如0x9406920691068F ...我想将其导入到python pycharm中以获取相同的确切数据类型。 However, it shows something like this instead [b'\\x94\\x06\\x92\\x06\\x91\\x06\\x8f\\x06\\x8d.. how do I copy the same numbers to python? 但是,它显示的是类似的内容[b'\\ x94 \\ x06 \\ x92 \\ x06 \\ x91 \\ x06 \\ x8f \\ x06 \\ x8d ..如何将相同的数字复制到python? I am a beginner in python, please help. 我是python的初学者,请帮忙。

I copied the code from previous post and it didn't work 我复制了上一篇文章中的代码,但没有用

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()

如果列返回类型为varbinary(max)则需要添加输出转换器函数来处理SQL_VARBINARY ,而不是SQL_BINARY

cnxn.add_output_converter(pyodbc.SQL_VARBINARY, converter_function_name)

暂无
暂无

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

相关问题 如何将退格\\ x08应用于字符串? - How to apply backspace \x08 to a string? 类型错误:不支持 unicode 字符串,请编码为字节:'\\x08' - TypeError: unicode strings are not supported, please encode to bytes: '\x08' 原因:python字符串赋值意外地将'\\ b'更改为'\\ x08'而将'\\ a'更改为'\\ x07',为什么Python会这样做? - the reason: python string assignments accidentally change '\b' into '\x08' and '\a' into '\x07', why Python did this? how to store bytes like b'PK\x03\x04\x14\x00\x08\x08\x08\x009bwR\x00\x00\x00\x00\x00\x00\x00 to dataframe or csv in python - how to store bytes like b'PK\x03\x04\x14\x00\x08\x08\x08\x009bwR\x00\x00\x00\x00\x00\x00\x00 to dataframe or csv in python python如何将特殊字符串u'\\ x08'u'\\ x09'u'\\ x03'转换为8 9 3 - python how to convert Special string u'\x08' u'\x09' u'\x03' to 8 9 3 我如何将像 b'\\x08\\x00\\x00\\x00' 这样的 Python/socket 读取的雷达数据解码为数字 - How do I decode radar data read by Python/socket like b'\x08\x00\x00\x00' to numbers 为什么`print'1 \\ x08'`结果1同时`print'1 \\ x082'`结果为2? - Why `print '1\x08'` results 1 meanwhile `print '1\x082'` results 2? 如何在python中看起来像[8,200,1,16]的字节格式与看起来像b'\\ x08 \\ xc8 \\ x01 \\ x10 \\ xc8的字节格式之间转换 - How to convert between the byte format that looks like [8, 200, 1, 16] to the one that looks like b'\x08\xc8\x01\x10\xc8 in python 从串行连接中获取b'\\ x08'b'\\ x9e'b'\\ x1d'或b'\\ xe0'b'\\ xe0'b'错误 - Getting b'\x08'b'\x9e'b'\x1d' or b'\xe0'b'\xe0'b' errors from a serial connection 使用_ \\ x08 __ \\ x08_d \\ x08de \\ x08el \\ x08li \\ x08it \\ x08te \\ x08em在python中解码字符串 - Decoding a string in python … with _\x08__\x08_d\x08de\x08el\x08li\x08it\x08te\x08em in it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM