Python mysql-connector将一些字符串转换为bytearray

Mak*_*duk 6 python mysql sql pandas mysql-connector-python

我使用python3和pandas连接到一些sql数据库:

import pandas as pd
import mysql.connector

cnx = mysql.connector.connect(user='me', password='***',
                          host='***',
                          database='***')
df=pd.read_sql("select id as uid,refType from user where registrationTime>=1451606400",con=cnx)
cnx.close()
Run Code Online (Sandbox Code Playgroud)

我得到2列:id和refType,它们都是string类型(SQL术语中的varchar).但是,由于某种原因,refType列被正确导入为字符串,但是uid列被导入为bytearray.这是他们看起来的样子:

df.head()
Run Code Online (Sandbox Code Playgroud)
                                             uid  
Run Code Online (Sandbox Code Playgroud)

0 [49,54,54,57,55,54,50,55,64,97,110]
1 [49,54,54,57,55,54,50,56,64,105,111]
2 [ 49,48,49,53,51,50,51,50,57,53,57,5 ...
3 [57,53,52,52,56,57,56,56,49,50,57, 5 ...
4 [49,54,54,57,55,54,50,57,64,105,111]

                                         refType  
Run Code Online (Sandbox Code Playgroud)

0 adx_Facebook.IE_an_ph_u8 _-.cc-ch.gf.au-ret7.c ...
1 adx_Facebook.IE_io_ph_u4 _-.cc-gb.gf.au-toppay ...
2 ad_nan_1845589538__CAbroadEOScys _-.cc-ca.gf.a. ..
3 ad_offerTrialPay-DKlvl10-1009
4 adx_Facebook.IE_io_ph_u4 _-.cc-us.gf.au-topspe ...

这就是uid列应该看起来的样子:

[i.decode() for i in df['uid'][1:5]]
Run Code Online (Sandbox Code Playgroud)

['16697628 @ io','10153232959751867 @ fb','954489881295911 @ fb','16697629 @ io']

我不明白为什么它转换为bytearray,也不知道如何选择将其转换为字符串.我在互联网或熊猫文档中找不到任何关于它或类似问题的内容.当然,我总是可以在导入后将该列转换为字符串,但这不是首选,因为显示的sql查询只是一个示例,并且在实际表中可能有数百列不正确地导入为bytearrays.在手动找到那些列并转换为字符串的屁股上真的很痛苦

连接器本身输出相同的bytearray:

cursor = cnx.cursor()
cursor.execute('select id as uid,refType from user where registrationTime>=1451606400 LIMIT 1')
cursor.fetchall()`
Run Code Online (Sandbox Code Playgroud)

[(bytearray(b'16697627 @ an'),'adx_Facebook.IE_an_ph_u8 _-.cc-ch.gf.au-ret7.cr-cys.dt-all.csd-291215.-')

SQL数据库中列的数据类型是第一列(uid)的"Varchar(32)"和第二列(refType)的"Varchar(128)"

小智 0

这确实很奇怪。我想知道将参数“coerce_float=False”传递给 read_sql 函数是否有助于解决这种情况。