【问题标题】:Printing varbinary data in Python retrieve from mysql-connector query在 Python 中打印从 mysql-connector 查询中检索的 varbinary 数据
【发布时间】:2021-06-23 22:16:19
【问题描述】:

感谢在 Stackoverflow 中分享的所有知识。

我在打印使用 Python3 和 mysql_connector 库从 MySQL 数据库检索的 varbinary 数据时遇到问题。 varbinary 列 (RawTxt) 在 MySQL 中存储为 varbinary,在 Python 中检索时显示为括号 [] 内的数字列表。

我会先投射然后打印吗?我尝试在连接变量中添加“charset='utf8'”参数无济于事。不确定它是否甚至是字符集/编解码器问题。这似乎有点专业,因为我在互联网上找不到太多关于它的信息。

下面是代码,使用 df.head() 打印,然后使用 df.to_csv 发送到 .csv 文件时的样子

代码:

import mysql.connector as mysql
import pandas as pd

dbconn = mysql.connect(
    host = '1234.com',
    user = 'root',
    password = '1234',
    database = 'tq_verbatims'
)

dbcursor = dbconn.cursor()
dbcursor.execute("""
SELECT a.RawID, b.RawTxt, c.GibberishOdds, a.cntResp 
FROM tq_verbatims.tblRawSpellingsCoded a, tq_verbatims.tblRawSpellings b, tq_verbatims.tblGibberishOdds c 
WHERE a.RawID = b.RawID AND b.SanitizedRawID = c.SanitizedRawID;
"""
)

tblRawSpellingCoded = dbcursor.fetchall()
df = pd.DataFrame(tblRawSpellingCoded, columns=dbcursor.column_names)
df.to_csv (r'C:\Users\export_dataframe.csv', index = False, header=True)
df.head()

df.head() 结果:

  RawID   RawTxt      GibberishOdds   cntResp
0   116 [40, 85, 78, 75, 78, 79, 87, 78, 41]    0.050513    1
1   237 [46, 46, 46, 46, 46, 46]    2.718280    1
2   284 [46, 78, 79, 84, 32, 83, 85, 82, 69]    0.079919    1
3   585 [51, 32, 77]    2.718280    6
4   658 [51, 77]    2.718280    403

在 .csv 文件中,RawTxt 字段在 .csv 文件输出中显示为 bytearray(b'(UNKOWN)'),其中应该只有文本“UNKNOWN”。这 4 行的 RawTxt 实际数据是 'UNKNOWN', '......', 'NOT SURE', '3 M'

RawID, RawTxt, GibberishOdds, cntResp
116,bytearray(b'(UNKNOWN)'),0.0505134,1
237,bytearray(b'......'),2.71828,1
284,bytearray(b'.NOT SURE'),0.0799194,1
585,bytearray(b'3 M'),2.71828,6

提前致谢。

【问题讨论】:

    标签: python dataframe mysql-connector varbinary


    【解决方案1】:

    RawTxt 字段导出为bytearray 的原因是cursor 将该字段中的值作为bytearray 值返回。在不知道表的确切架构的情况下,很难解释为什么 RawTxt 会被 mysql-connector 包转换为 bytearray

    无论如何,如果您确定RawTxt 包含有效的字符串值,那么您可以在将数据传递给Pandas 之前或之后将这些值转换为字符串。

    Pandas之前:
    import mysql.connector as mysql
    import pandas as pd
    
    dbconn = mysql.connect(
        host = '1234.com',
        user = 'root',
        password = '1234',
        database = 'tq_verbatims'
    )
    
    dbcursor = dbconn.cursor()
    dbcursor.execute("""
    SELECT a.RawID, b.RawTxt, c.GibberishOdds, a.cntResp 
    FROM tq_verbatims.tblRawSpellingsCoded a, tq_verbatims.tblRawSpellings b, tq_verbatims.tblGibberishOdds c 
    WHERE a.RawID = b.RawID AND b.SanitizedRawID = c.SanitizedRawID;
    """
    )
    tblRawSpellingCoded = []
    for row in dbcursor.fetchall():
        row[1] = row[1].decode('utf8')
        tblRawSpellingCoded.append(row)
    df = pd.DataFrame(tblRawSpellingCoded, columns=dbcursor.column_names)
    df.to_csv (r'C:\Users\export_dataframe.csv', index = False, header=True)
    df.head()
    
    Pandas之后:
    import mysql.connector as mysql
    import pandas as pd
    
    dbconn = mysql.connect(
        host = '1234.com',
        user = 'root',
        password = '1234',
        database = 'tq_verbatims'
    )
    
    dbcursor = dbconn.cursor()
    dbcursor.execute("""
    SELECT a.RawID, b.RawTxt, c.GibberishOdds, a.cntResp 
    FROM tq_verbatims.tblRawSpellingsCoded a, tq_verbatims.tblRawSpellings b, tq_verbatims.tblGibberishOdds c 
    WHERE a.RawID = b.RawID AND b.SanitizedRawID = c.SanitizedRawID;
    """
    )
    
    tblRawSpellingCoded = dbcursor.fetchall()
    df = pd.DataFrame(tblRawSpellingCoded, columns=dbcursor.column_names)
    df.RawTxt = df.RawTxt.str.decode('utf8')
    df.to_csv (r'C:\Users\export_dataframe.csv', index = False, header=True)
    df.head()
    

    任一解决方案都应将RawTxt 值从bytearray 值转换为string,并假设这些值是有效的字符串值。

    【讨论】:

    • 该列作为 varbinary 存储在 MySQL 表中,因此它的输出是该类型。基本上,您是在说,“只需在导入时更改类型。”我绝对可以使用它。谢谢,阿卜杜。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 2015-08-26
    相关资源
    最近更新 更多