不同mysql数据库实例中的相同代码
sql_table_def = 'show create table {}.{}'.format(dbname,table_name)
df_table_def = pandas.read_sql_query(sql_table_def,self.conn)
create_table_sql = df_table_def.head(1)['Create Table'].values[0]但有时返回值很奇怪:
print(create_table_sql)
b"CREATE TABLE 在"CREATE TABLE"前面有一个b!
但是db实例的大部分返回值都是正确的:
print(create_table_sql)只返回"CREATE TABLE",前面没有b。
有什么问题吗?
发布于 2018-10-12 12:59:32
您可以使用.decode将bytes转换为str (如果您知道编码):
In [11]: b'string'
Out[11]: b'string'
In [12]: b'string'.decode("utf-8") # the encoding may not be utf-8...
Out[12]: 'string'
In [13]: type(b'string')
Out[13]: bytes
In [14]: type('')
Out[14]: str问题是数据库不知道编码,也不会猜测(这可能是一件好事)。尽管您可能会发现不支持utf-8以外的编码(例如,对于PyMySQL)...
https://stackoverflow.com/questions/52771800
复制相似问题