1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python +mysql + mediumtextで文字化け的おかしなことを直す

Posted at

Mysqlにpythonでつなぐと、varcharなら化けないのにmediumtextにすると化ける(ように見える)

解決策) bytes to str でOK
import mysql.connector

contents = row[2]
とすると↓
'\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86'
のようになったら、当該フィールドがなぜかbytesで読み込まれているので
contents = row[2].decode('utf-8')
とすれば文字化けしなくなります。というか文字化けじゃなくてbyteをそのままだそうとしたらそうなるわな。

疑問)なんでvarcharならstrで受けるのに、mediumtxtではbytesになるのかが謎。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?